Solved 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_OBJECT 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 ) public: Person(QObject *parent = 0); QString name() const; void setName(const QString &); int shoeSize() const; void setShoeSize(int); QList<int> age(); private: QString m_name; int m_shoeSize; QList<int> m_age; signals: void nameChanged(); }; #endif // PERSON_H
#include "person.h" #include <QList> Person::Person(QObject *parent) : QObject(parent), m_shoeSize(0) { setName("ERNIE"); 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; }
Rectangle { id:settingsWindow visible: true //visibility: "FullScreen" width: 800 height: 480 color:"#71469A" anchors.top: parent.top anchors.topMargin: 35 Component.onCompleted: { for (var i=0; i<3; i++) console.log("here") 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]") } }
-
@FreeRollen
The CPPQList
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 Javascriptcount
method butlength
. So use that instead.
More info here:
http://doc.qt.io/qt-5/qtqml-cppintegration-data.html#sequence-type-to-javascript-array