Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QStyledItemDelegate: getting QModelIndex::data value by Qt::UserRole in paint() method [Invalid]
Forum Updated to NodeBB v4.3 + New Features

QStyledItemDelegate: getting QModelIndex::data value by Qt::UserRole in paint() method [Invalid]

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    janfaroe
    wrote on last edited by
    #1

    Hi all,

    I have a subclassed QStyledItemDelegate class where I do some extra painting jobs. I'm setting the delegate on a QTreeWidget, to which I am of course adding QTreeWidgetItems to. I had no problems getting the text of the item by index.data(Qt::DisplayRole). However, wanting to be a bit more fancy, I'm adding more data to each QTreeWidgetItem with QTreeWidgetItem::setData(column, Qt::UserRole, someValue).

    How do I get those values in my QStyledItemDelegate:: paint method? I tried with QModelIndex(Qt::UserRole).toInt() (after checking if I'm at the right rolumn), but I'm not getting the values I'm expecting. I am probably doing something wrong here, but what?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DBoosalis
      wrote on last edited by
      #2

      What getting a handle to the item itself, something like this:

      MyItem::paint(QPainter *painter,
      const QStyleOptionViewItem &option,
      const QModelIndex &index) const
      {
      QStandardItemModel *model = (QStandardItemModel *) index.model();
      QStandardItem *item = model->itemFromIndex(index);
      QVariant v = item->data( Qt::DecorationRole ); // or UserRole in your case
      ..
      }

      Hope this is of some value to you

      1 Reply Last reply
      0
      • J Offline
        J Offline
        janfaroe
        wrote on last edited by
        #3

        No, just an int value set via QTreeWidgetItem::setData.

        Sorry, seems like it was my own mistake. I was manipulating data other were, and that tricked me into believing my method of fetching data was wrong.

        Ignore me, please.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved