Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Unsolved QList<QObject *> data model roles in QML. Ok in example and not Ok in real life.

    QML and Qt Quick
    2
    2
    833
    Loading More Posts
    • 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.
    • K
      Kofr last edited by Kofr

      That is my QML code instantly,

      ComboBox {
              ...
              textRole: "categoryName"
      
              model: converter.categoryModel
      
              onCurrentIndexChanged : {
                  console.log(model.modelData.name)
                  //converter.setSourceUnitModel(model.category)
              }
      ...
      
      

      ComboBox sees "categoryName" and shows data, but when I try to access any other role ("model.modelData.name") it can not access in any modification of this string.
      Here are properties:

      Q_PROPERTY(Category category READ category WRITE setCategory NOTIFY categoryChanged)
          Q_PROPERTY(double coefficient READ coefficient WRITE setCoefficient NOTIFY coefficientChanged)
          Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
          Q_PROPERTY(QString categoryName READ categoryName WRITE setCategoryName NOTIFY categoryNameChanged)
      

      error:
      TypeError: Cannot read property 'name' of undefined
      what is wrong?

      Example copy-past:

      ListView {
          width: 100; height: 100
      
          model: myModel
          delegate: Rectangle {
              height: 25
              width: 100
              color: model.modelData.color
              Text { text: name }
          }
      }
      

      and in this case when MyModel is registered with qqmlcontext property let access to all the roles this way.
      So, again, what is wrong with my code?

      p3c0 1 Reply Last reply Reply Quote 0
      • p3c0
        p3c0 Moderators @Kofr last edited by

        @Kofr AFAIK It works in ListView's delegate only because properties of model are made available for it. It won't work outside of the delegate and thus in case of ComboBox modelData wont work.
        To access the model data you can use get (for ListModel ) and passing it in your case currentIndex. For C++ model you can write your own Q_INVOKABLE function.

        157

        1 Reply Last reply Reply Quote 0
        • First post
          Last post