Results 1 to 3 of 3

Thread: Item delegate displaying enumerated values

  1. #1
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Item delegate displaying enumerated values

    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)

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Item delegate displaying enumerated values

    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.

  3. The following user says thank you to ChrisW67 for this useful post:

    mclark (24th September 2010)

  4. #3
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Item delegate displaying enumerated values

    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.
    Last edited by mclark; 24th September 2010 at 20:12.

Similar Threads

  1. Item Delegate Visibility
    By raneeshambat in forum Newbie
    Replies: 1
    Last Post: 18th September 2009, 20:41
  2. Replies: 5
    Last Post: 10th August 2009, 10:50
  3. Delegate for a certain item?
    By somebody in forum Qt Programming
    Replies: 1
    Last Post: 18th August 2008, 22:55
  4. Replies: 1
    Last Post: 25th July 2008, 08:38
  5. Item Delegate Painting
    By stevey in forum Qt Programming
    Replies: 3
    Last Post: 9th May 2008, 07:37

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.