Why can my Q_GADGET
be read perfectly in QML (JS) but not my Q_OBJECT
?
Running Qt 5.8.0 on Ubuntu 14.04.
I'm attempting to return a list (QVariantMap
) of objects to QML. I'm keeping it simple right now, no pointers, etc.. just copies of the objects.
struct Blob
{
Q_GADGET
Q_PROPERTY(QString uuid MEMBER uuid_ CONSTANT)
Q_PROPERTY(QVector3D centroid MEMBER centroid_)
// ...
Blob() {};
~Blob() = default;
Blob(const Blob& blob);
};
Q_DECLARE_METATYPE(Blob)
There are a bunch of QString
and QVector3D
members on Blob
.
In main.cpp, I also register the type:
qmlRegisterType<Blob>("matt", 1, 0, "Blob");
Then my JS code is able to read all the properties (for loop iterating over them) on the object without issue.
But if I use a Q_OBJECT
struct Blob : public QObject
{
Q_OBJECT
Q_PROPERTY(QString uuid MEMBER uuid_ CONSTANT)
Q_PROPERTY(QVector3D centroid MEMBER centroid_)
// ...
explicit Blob(QObject parent = nullptr) : QObjecT(parent) {};
~Blob() = default;
Blob(const Blob& blob);
};
Q_DECLARE_METATYPE(Blob)
Then the JS receives an object where all the properies are the keys from the QVariantMap
, but whose values are empty objects.
Note, before sending it to the JS, I convert it to a QVariant and back again just to confirm that that works, e.g.
Blob b;
qDebug() << "Created: " << b;
QVariant var = QVariant::fromValue(b);
qDebug() << " Converted back = " << var.value<Blob>().toQString();
I'd prefer to use a Q_OBJECT
such that I have slots/signals.