Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Accessing a role in a c++ model from qml TableView ItemDelegate
Qt 6.11 is out! See what's new in the release blog

Accessing a role in a c++ model from qml TableView ItemDelegate

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 2 Posters 2.1k Views
  • 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.
  • denishessbergerD Offline
    denishessbergerD Offline
    denishessberger
    wrote on last edited by
    #1

    Hello there!

    I am trying to directly access a role in a c++ model from within my ItemDelegate in a table view. All the solutions I have found so far do not work.

    Basically I want to set the color for rectangles in the first two columns in a TableView based on data in the c++ model. As the color might be different in each row, I need to do this from within the ItemDelegate. I know how to check for the column number using styleData.col, but I am unable to access the models "user_color" role using

    color: tableView.model[styleData.row].user_color

    as this leads me to
    TypeError: Cannot read property 'user_color' of undefined

    Seeing that this part is pretty poorly documented, I am hoping for some help in here :)

    Thanks,

    Denis

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vladstelmahovsky
      wrote on last edited by
      #2

      Hi
      this is how it works for me:

                      itemDelegate: Text {
                          text: styleData.role === "prio" ? filterModel.getPrio(styleData.row) : styleData.value
                          color: filterModel.getColor(styleData.row)
                      }
      
      

      i.e role attached to styleData.role in itemDelegate

      1 Reply Last reply
      0
      • denishessbergerD Offline
        denishessbergerD Offline
        denishessberger
        wrote on last edited by
        #3

        But this implies you have a getColor function in your c++ model?

        denishessbergerD 1 Reply Last reply
        0
        • denishessbergerD denishessberger

          But this implies you have a getColor function in your c++ model?

          denishessbergerD Offline
          denishessbergerD Offline
          denishessberger
          wrote on last edited by
          #4

          Actually... As i figured out in another thread in here it seems this is the way to go (QT5.5, btw):

          itemDelegate: Rectangle
          {
          height: rowHeight
          color: model["user_color"] ? model["user_color"] : "transparent"
          }

          And it also auto updates, when the underlaying data changes.

          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