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. QList<QObject *> data model roles in QML. Ok in example and not Ok in real life.

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

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 1.0k Views
  • 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 Offline
    K Offline
    Kofr
    wrote on last edited by Kofr
    #1

    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?

    p3c0P 1 Reply Last reply
    0
    • K 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?

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @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
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved