Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to access list property of C++ object in QML
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 1.7k 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.
  • S Offline
    S Offline
    strasidlo
    wrote on last edited by
    #1

    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;;
    

    }
    @

    1 Reply Last reply
    0
    • S Offline
      S Offline
      stevenceuppens
      wrote on last edited by
      #2

      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,

      Steven CEUPPENS
      Developer &#x2F; Architect
      Mobile: +32 479 65 93 10

      1 Reply Last reply
      0
      • S Offline
        S Offline
        strasidlo
        wrote on last edited by
        #3

        Sorry, I have updated it.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          stevenceuppens
          wrote on last edited by
          #4

          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.

          Steven CEUPPENS
          Developer &#x2F; Architect
          Mobile: +32 479 65 93 10

          1 Reply Last reply
          0
          • S Offline
            S Offline
            strasidlo
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • T Offline
              T Offline
              terenty
              wrote on last edited by
              #6

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

              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