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

  • 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 :)



  • 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

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

  • 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.

Log in to reply