January 16, 2011

Jonathan Jonathan
Lab Rat
30 posts

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:

  1. modify the text in the QLineEdit I use for my QItemDelegate
  2. 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

  1. navigate away from my “compound” editor or if I press return
  2. 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.

5 replies

February 15, 2011

Darryl DSouza Darryl DSouza
Lab Rat
34 posts

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.

 Signature 

Darryl D’Souza

February 15, 2011

Volker Volker
Ant Farmer
5331 posts

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:

February 15, 2011

Jonathan Jonathan
Lab Rat
30 posts

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?

February 15, 2011

Volker Volker
Ant Farmer
5331 posts

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.

February 16, 2011

Darryl DSouza Darryl DSouza
Lab Rat
34 posts

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.

 Signature 

Darryl D’Souza

 
  ‹‹ Cmd-H shortcut on Mac OS      transformer.cpp:10: error: undefined reference to `vtable for Transformer’ ››

You must log in to post a reply. Not a member yet? Register here!