PDA

View Full Version : Model/View framework, delegate: don't close editor while it contains invalid value



stillwaiting
25th May 2011, 12:02
Dear friends,

Is it possible to obtain the next behavior?

- user starts editing view's cell;
- when he/she finishes, delegate (or who?) checks is the new value is correct;
-- if YES: save data to model;
-- if NO: show message box "Data is invalid" and leave editor and the focus in it so user could fix the value at the same moment.

I tried to use event filter installed on the editor (created by delegate::createEditor()) by handling FocusOut events, but when user clicks the other cell in the view the current editor gets closed anyway and one creates new editor for the clicked cell.

Santosh Reddy
25th May 2011, 16:21
Yes, it is possible.

You need have sub-class of QItemDelegate, and your data validation need to go into implementation of QItemDelegate::setModelData(), where the data has to be written to the model, you can validate and throw Model dialogs from there.

stillwaiting
26th May 2011, 04:25
Yes, it is possible.

You need have sub-class of QItemDelegate, and your data validation need to go into implementation of QItemDelegate::setModelData(), where the data has to be written to the model, you can validate and throw Model dialogs from there.

But how can I prevent current editor from being closed? I want user to continue editing after he closes the "error" dialog.

Santosh Reddy
26th May 2011, 05:30
Try QAbstractItemDelegate::editorEvent() to handle event from view


I tried to use event filter installed on the editor (created by delegate::createEditor()) by handling FocusOut events, but when user clicks the other cell in the view the current editor gets closed anyway and one creates new editor for the clicked cell.

I think you should handle QEvent::Close for the editor widget.

wysota
26th May 2011, 09:30
But how can I prevent current editor from being closed? I want user to continue editing after he closes the "error" dialog.

So reopen the editor instead of trying to make it remain open.

Santosh Reddy
26th May 2011, 09:34
So reopen the editor instead of trying to make it remain open.
wysota,

Do you think it is possible to start editor from a delegate? I guess only view has a control to start edting

wysota
26th May 2011, 09:42
So pass the pointer to the view to the delegate. Besides, the delegate has a pointer to the view but it is hidden in the docs because it is easy to abuse it.

stillwaiting
26th May 2011, 09:42
Try QAbstractItemDelegate::editorEvent() to handle event from view



I think you should handle QEvent::Close for the editor widget.

Thank you for you suggestions but... no success :( when I clicks the other cell one closes the current editor and shows the new one.


I implemented the workaround by installing the event filter on "QApplication::instance()": when value is not valid then the filter blocks mouse and keyboard events which can change the focus (MouseClick, KeyUp, KeyDown, KeyTab etc) and shows error dialog.

Looks like ugly hack but that's the best idea I had.

Santosh Reddy
26th May 2011, 11:04
The documentation says


void QAbstractItemDelegate::closeEditor ( QWidget * editor, QAbstractItemDelegate::EndEditHint hint = NoHint ) [signal]
This signal is emitted when the user has finished editing an item using the specified editor.
The hint provides a way for the delegate to influence how the model and view behave after editing is completed. It indicates to these components what action should be performed next to provide a comfortable editing experience for the user. For example, if EditNextItem is specified, the view should use a delegate to open an editor on the next item in the model.

which means that signal to close to editor is emitted by the QAbstractItemDelegate (base class), we don't have any control over this, basically we cannot stop editor being closed from delegate, unless we block all signals from the delegate which is not practical

another way (other than event filtering) would be to send a signal to the QAbstractItemView::edit () slot of the view to restart the editor (after validating the data)

wysota
26th May 2011, 13:29
another way (other than event filtering) would be to send a signal to the QAbstractItemView::edit () slot of the view to restart the editor (after validating the data)
That's exactly what I meant.