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. Depends on non-NOTIFYable properties
Qt 6.11 is out! See what's new in the release blog

Depends on non-NOTIFYable properties

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 4.8k 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.
  • M Offline
    M Offline
    Milosz
    wrote on last edited by
    #1

    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
    0
    • Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on last edited by
      #2

      @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
      1
      • M Offline
        M Offline
        Milosz
        wrote on last edited by
        #3

        It works - thanks a lot Gojir4

        1 Reply Last reply
        1

        • Login

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