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.
You need a user property [doc.qt.nokia.com] in your compound widget, so that the delegate can set and retrieve the data automagically.
See the docs of:
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.