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