Noob question about Properties and Scopes



  • I'm totally new into the Qt and qml stuff and I just waste my all day trying to understand the property scopes without success. So I have a Table View with an Item delegate like this:

    @
    TableView {
    id: logView
    model: logModel
    TableViewColumn{
    role:"idLog"
    }
    itemDelegate: Item {
    id: logItem
    property int idLog: -1
    Rectangle {
    width:50
    height:20
    MouseArea{
    anchors.fill: parent
    onClicked: console.log( parent.parent.idLog) //always -1
    //onClicked: console.log( logItem.idLog)
    }
    }

          Component.onCompleted: {
                if (styleData.role === "idLog"){
                   console.log(styleData.value) // 1 and 2
                   idLog = styleData.value
             }
          }
      }
    

    Component.onCompleted: {
    logModel.append({"idLog":1})
    logModel.append({"idLog":2})
    }
    }
    @

    And all I want is to save the id into the item prop so I can retrieve it after when I click the rect. I always get -1. Why is that?


  • Moderators

    See "this":http://qt-project.org/doc/qt-5.0/qtquick/qtquick-modelviewsdata-modelview.html#mouse-and-touch-handling. You should not be saving the stuff in a delegate, as the engine is allowed to create and delete those components as they are needed.



  • Thank you! after proper reading I can see now how nasty is to try saving things in a delegate. So now what I'm doing is to simply access the model directly in this way:

    @
    onClicked: {
    var model = logModel.get(styleData.row)
    console.log("idLog",model.idLog) //prints correct id
    }
    @
    But now my question is this: can I expect to have always a correlation between the row and the original object index? I tried removing elements and it seems that the model keeps track correctly but, should I be aware of a better way of doing this? gotchas?


  • Moderators

    Hm, that is a good question. I don't think you should depend upon it, as it is possible to remove an item from the middle of the list - the index might jump then. But I am not sure. If you have a use case where you are certain the list will not be reordered, and that nothing will be removed from it, I think you can assume the index will stay valid.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.