Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Depends on non-NOTIFYable properties



  • 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



  • @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()



  • It works - thanks a lot Gojir4


Log in to reply