Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing C++ QLists from QML

If I've got a list of things in C++, how do I expose that to QML (in Qt5 / QtQuick 2)? It seems like QML can only understand QObject-derived classes, which is an issue because QObjects can't be put in a QList or copied. How do I do this:

struct Thing {     int size;     QString name; };  class ThingManager : public QObject {     Q_OBJECT      // These macros support QtQuick, in case we one day want to use it to make a slick     // interface (when QML desktop components are released).     Q_PROPERTY(QList<Thing> things READ things NOTIFY thingssChanged)  public:     // ...     QList<Thing> things() const;      // ...  }; 

So that I can do something like this in QML:?

var a = thingManager.things[0].name; 
like image 715
Timmmm Avatar asked Jan 11 '13 21:01

Timmmm


1 Answers

Alternatively, You can use QVariantList (QList<QVariant>), it will automatically change to JavaScript array when passed to QML, and it is read and write-able from C++ and QML

like image 110
Dickson Avatar answered Sep 25 '22 20:09

Dickson