Exporting C++ QList<int> to QML

  • I am new to qt quick and I a create a simple C++ class with Q_PROPERTY(QList<int> age READ age ). I want to be able to access the age.count from my qml so and can print all the ages out but i am getting an error saying.. qrc:/settings.qml:26: TypeError: Property 'count' of object 1,2,3,5 is not a function;

    #ifndef PERSON_H
    #define PERSON_H
    #include <QObject>
    class Person : public QObject
        Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
        Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize)
        Q_PROPERTY(QList<int> age READ age  )
        Person(QObject *parent = 0);
        QString name() const;
        void setName(const QString &);
        int shoeSize() const;
        void setShoeSize(int);
        QList<int> age();
        QString m_name;
        int m_shoeSize;
        QList<int> m_age;
        void nameChanged();
    #endif // PERSON_H
    #include "person.h"
    #include <QList>
    Person::Person(QObject *parent)
    : QObject(parent), m_shoeSize(0)
        m_age.append( 1 );
        m_age.append( 2 );
        m_age.append( 3 );
        m_age.append( 5 );
    QString Person::name() const
        return m_name;
    void Person::setName(const QString &n)
        m_name = n;
        emit nameChanged();
    int Person::shoeSize() const
        return m_shoeSize;
    void Person::setShoeSize(int s)
        m_shoeSize = s;
    QList<int> Person::age()
        return m_age;
        visible: true
        //visibility: "FullScreen"
        width: 800
        height: 480
        anchors.top: parent.top
        anchors.topMargin: 35
        Component.onCompleted: {
             for (var i=0; i<3; i++)
             console.log("index 0: " + mPeeps.age[0])
             console.log("index 1: " + mPeeps.age[2])
             console.log("index 3: " + mPeeps.age[3])
             console.log("count: " + mPeeps.age.count() )
             //for (var w=0; w<mPeeps.age.count(); w++)
            //     console.log("items[i]")

  • Moderators

    The CPP QList of variants gets converted to Javascript Arrays automcatically when exposed to QML. So since in QML it is identified as an array, there is no Javascript count method but length. So use that instead.
    More info here:

Log in to reply

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