A QItemDelegate that uses my own editor QWidget can fail to update the model
-
I'm using a QTableView and a QSqlRelationalTableModel. I have successfully implemented QItemDelegates that use a QDateTimeEdit and QLineEdit.
In particular if I:
modify the text in the QLineEdit I use for my QItemDelegate
call submitAll() on the model
then QItemDelegate::setModelData() is correctly called. ie the model is updated and the modified text saved to the database.
However, if instead my QItemDelegate editor is a "compound" editor, say a QWidget to which I've added a QLineEdit (rather than just a QLineEdit by itself), then setModelData() isn't called in the above circumstances.
On the other hand if I
navigate away from my "compound" editor or if I press return
call submitAll()
then setModelData() is correctly called.
Any ideas on why my "compound" editor isn't fully working? I presume that if I use a simple editor the model knows that the data in the editor has changed, but if I use a compound editor the necessary events are being blocked.
-
Yes. You know the reason right yourself:)
If you have the custom widget how the Qt will know when the data is changed? You must call commitData().
See void QAbstractItemDelegate::commitData ( QWidget * editor ) function. -
You need a "user property":http://doc.qt.nokia.com/4.7/properties.html in your compound widget, so that the delegate can set and retrieve the data automagically.
See the docs of:
- "QStyledItemDelegate::createEditor() ":http://doc.qt.nokia.com/4.7/qstyleditemdelegate.html#createEditor
- "QStyledItemDelegate::setEditorData() ":http://doc.qt.nokia.com/4.7/qstyleditemdelegate.html#setEditorData
- "QStyledItemDelegate::setModelData() ":http://doc.qt.nokia.com/4.7/qstyleditemdelegate.html#setModelData
-
Darryl: How do I obtain a reference to the editor in order to call ::commitData? The methods in the delegate are all "consts".
Volker: Are you suggesting that I create a user property in my compound widget (ie the editor) to store a reference to the delegate? When and by whom is this property (i) set, (ii) retrieved?
-
Define a property for your compound widget that holds the value the user has entered. Whatever it is - we don't know. Provide a read and a write method and mark it as user property, QStyledItemDelegate will recognize it and use it to set the models value as default for the editor and to retrieve the value the user has entered and store that one in the model. You only will have to return a new instance of your widget in the overwritten method createEditor() of your delegate. See the links provided, they tell you who calls the property.
-
Another simple way could be catch the signal of your compound widget QLineEdit which is a child of QWidget on user update finished and in that slot, you can call commitData(). Qt now must call setModelData() function.