Qt Forum

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

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved Depends on non-NOTIFYable properties

    QML and Qt Quick
    2
    3
    2786
    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.
    • M
      Milosz last edited by

      I have implemented c++ class

      class GameList : public QAbstractListModel
      {
      public:
          GameList(QObject *parent = nullptr);
      
          int rowCount(const QModelIndex &parent = QModelIndex()) const override;
          QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
      
      private:
          void fillGameList() noexcept;
      
      private:
          QList<QString> m_gameList;
      };
      
      int GameList::rowCount(const QModelIndex &parent) const
      {
          if(parent.isValid())
              return 0;
      
          return m_gameList.size();
      }
      
      QVariant GameList::data(const QModelIndex &index, int role) const
      {
      
          if (!index.isValid())
              return QVariant();
      
          if (index.row() >= m_gameList.size() || index.row() < 0)
              return QVariant();
      
          if(role == Qt::DisplayRole)
          {
              return m_gameList.at(index.row());
          }
          return QVariant();
      }
      

      and QML like that:

      Window {
          visible: true
          width: 640
          height: 480
          title: qsTr("Hello World")
      
          Row {
              Repeater{
                  model: gameList
                  Text{ text: data + "  " }
              }
          }
      }
      

      I expect that I get data items (there are QString type) but on the form I have [object QObject] [object QObject] ....
      and error: QQmlExpression: Expression qrc:/main.qml:13:25 depends on non-NOTIFYable properties:
      QQuickText::data

      What is wrong with this code?

      Thanks in advance

      1 Reply Last reply Reply Quote 0
      • Gojir4
        Gojir4 last edited by

        @milosz Hi,

        data is a property available from type Item in QML. You need to use display keyword to access "data" from the model.

        Text{ text: display }
        

        This is documented here QAbstractItemModel::roleNames()

        Alternatively you can provide your own role names by overriding QAbstractItemModel::roleNames()

        1 Reply Last reply Reply Quote 1
        • M
          Milosz last edited by

          It works - thanks a lot Gojir4

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