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. Model property set in listview: delegate can't find properties.
Forum Updated to NodeBB v4.3 + New Features

Model property set in listview: delegate can't find properties.

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 3 Posters 5.5k 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.
  • B Offline
    B Offline
    bkamps
    wrote on last edited by
    #1

    Hi all,

    Let's say I have a window (rectangle) that has a widget (listview). When I do the following everything is working ( all names of contacts are print):

    Window.qml:

    @Rectangle {

    ContactWidget { model: myModel }
    

    }@

    ContactWidget.qml:

    @ListView {
    delegate: Text { text: modelData.name }
    }@

    When I set the model property within the ContactWidget it doesn't work:

    Window.qml:

    @Rectangle {
    ContactWidget {}
    }@

    ContactWidget.qml:

    @ListView {
    model: myModel
    delegate: Text { text: myModel.name }
    }@

    I get the following message:

    Unable to assign [undefined] to QString text.

    The model is a derived QAbstractListModel and the name property is a string...

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

      How do you pass your myModel to qml?

      1 Reply Last reply
      0
      • B Offline
        B Offline
        bkamps
        wrote on last edited by
        #3

        It's a derived C++ class of QAbstractListModel. I pass it by:

        setContextProperty("myModel",objectDerivedListModel);

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mbrasser
          wrote on last edited by
          #4

          Hi,

          In the second case it looks like you are binding to myModel.name, which looks for a name property on the model itself (rather than for each individual row in the model). If modelData.name works for the first case, you should be able to use it for the second as well (rather than myModel.name).

          If you haven't seen it already, the documentation for the specialized roles available within a model delegate are documented at http://doc.trolltech.com/4.7-snapshot/qdeclarativemodels.html .

          Regards,
          Michael

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DenisKormalev
            wrote on last edited by
            #5

            Ah, yep, didn't notice myModel in second case. Changing it to modelData should help you.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              bkamps
              wrote on last edited by
              #6

              That did the trick.. Very strange because I tried this before but then it didn't work.

              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