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. Noob question about Properties and Scopes
Forum Updated to NodeBB v4.3 + New Features

Noob question about Properties and Scopes

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 1.1k 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.
  • R Offline
    R Offline
    radixzz
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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.

      (Z(:^

      1 Reply Last reply
      0
      • R Offline
        R Offline
        radixzz
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          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.

          (Z(:^

          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