How to access list property of C++ object in QML



  • Hello, I have started to learn the Qt 5 and got stuck at one point for a long time.

    I have two classes. ScoreHandler handles a list of ScoreRecords. I create those two classes in C++ and set an instance of ScoreHandler as a context property. Now in QML I can assign the model, but the delegate can not see properties of ScoreRecord. Do I have to register something somewhere? Please help me.

    scorerecord.h
    @
    class ScoreRecord : public QObject
    {
    Q_OBJECT

    Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
    Q_PROPERTY(QString date READ date WRITE setDate NOTIFY dateChanged)
    Q_PROPERTY(QString score READ score WRITE setScore NOTIFY scoreChanged)
    

    public:
    ScoreRecord(QObject *parent = 0);
    ScoreRecord(const QString& n, const QString &d, const QString &s, QObject *parent = 0);
    QString name() const;
    void setName(const QString &str);
    QString date() const;
    void setDate(const QString &str);
    QString score() const;
    void setScore(const QString &str);

    signals:
    void nameChanged();
    void dateChanged();
    void scoreChanged();

    public slots:

    private:
    QString m_name;
    QString m_date;
    QString m_score;
    };
    @

    scorehandler.h

    @
    class ScoreHandler : public QObject
    {
    Q_OBJECT
    private:
    const char* SCORE_TABLE_FILENAME;

    struct scoreRow {
        char name[128];
        char date[32];
        char score[16];
    };
    

    public:
    explicit ScoreHandler(QObject *parent = 0);
    QList<ScoreRecord *> scoreList;

    signals:

    public slots:
    void SaveScore(const QString &name, const QString &date, const QString &score);
    void LoadScore();
    };
    @

    main.c
    @
    int main(int argc, char *argv[])
    {

    QGuiApplication app(argc, argv);
    QtQuick2ApplicationViewer viewer;
    
    ScoreHandler* scoreHandler = new ScoreHandler();
    QQmlContext* ctx = viewer.rootContext();
    ctx->setContextProperty("MyScoreModel", QVariant::fromValue(scoreHandler->scoreList));
    viewer.setMainQmlFile&#40;QStringLiteral("qml/qmlListView/main.qml"&#41;&#41;;
    
    viewer.showExpanded(&#41;;
    
    return app.exec&#40;&#41;;
    

    }
    @

    QML file
    @
    import QtQuick 2.0

    Rectangle {
    width: 360
    height: 360

    ListView {
        width: 100; height: 100
        anchors.fill: parent
    
        model: MyScoreModel
        delegate: Text {
            text: name    // Here the JS interpreter prints error: ReferenceError: name is not defined
        }
    }
    

    }
    @

    And please can somebody explain me, why the following code works? What is the reason?

    main.c
    @
    int main(int argc, char *argv[])
    {

    QGuiApplication app(argc, argv);
    QtQuick2ApplicationViewer viewer;
    
    QList<QObject *> scoreList;
    
    scoreList.append(new ScoreRecord("Jmeno1", "datum1", "score1"));
    scoreList.append(new ScoreRecord("Jmeno2", "datum2", "score2"));
    scoreList.append(new ScoreRecord("Jmeno3", "datum3", "score3"));
    
    QQmlContext* ctx = viewer.rootContext();
    ctx->setContextProperty("MyScoreModel", QVariant::fromValue(scoreList));
    viewer.setMainQmlFile&#40;QStringLiteral("qml/qmlListView/main.qml"&#41;&#41;;
    
    viewer.showExpanded(&#41;;
    
    return app.exec(&#41;;
    

    }
    @



  • Hi,

    In your code examples above, you forgot to show the code of scorehandler.h, it's twice the source code of scorerecord.h.

    can you first update it? than i will have a look at it.

    Rgds,



  • Sorry, I have updated it.



  • in class ScoreHandler you have the variable

    QList<ScoreRecord *> scoreList;

    which you expose to QML, ScoreRecord is derived from QObject, but QML does not handle inheritance for this,

    so change line 16 to QList<QObject *> scoreList;

    other things seems ok...

    alternatively, instead of using the QList<QObject*> style, you can create a real QAbstractItemModel.



  • Ahaaa, that was the thing. Thank you very much. I will do some experiments with QAbstractItemModel later and probably will come back with another problems. So far, thank you gain.



  • Hi, See examples\quick\models\abstractitemmodel in your Qt installation for an example on custom QAbstractItemModel


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.