PDA

View Full Version : Item delegate displaying enumerated values



mclark
23rd September 2010, 17:41
I have a model that stores the index of an enumeration in an item. My tree view has a QStyledItemDelegate subclass that correctly allows editing using a QComboBox filled with the enum strings.

My problem is that I would like to display the enum text instead of the index when NOT editing the row.

Adding to the problem is that the model is used by multiple views so storing the data as the enum string instead of the index will require more work. And, being a programmer, I am against having to do more work than absolutely necessary ;)

I have looked at overriding the paint() function but it looks like the data cannot be accessed or changed (all those darned const's). So, it looks like my only option is to change the data value in the model:(

Does anyone have any suggestions as to how I may accomplish this, or should I just suck it up and change the model to use the enum string in all the views?

Qt Commercial 4.6.2
Windows (XP SP3 / 7)

ChrisW67
24th September 2010, 07:18
Return the relevant string for the Qt::DisplayRole in your model's data() method. You can leave the index number on the Qt::EditRole for anything that needs it for editing purposes.

mclark
24th September 2010, 17:31
Yes, of course! It works just as you suggested. I knew there had to be an easy way to accomplish this.

Thank you for your help.

Users should also note the documentation for QStandardItem::setData() and data():
Note: The default implementation treats Qt::EditRole and DisplayRole as referring to the same data.