Discussion:
[Interest] Q_GADGET vs Q_OBJECT
Russell, Matthew
2017-04-13 18:31:23 UTC
Permalink
Why can my Q_GADGET be read perfectly in QML (JS) but not my Q_OBJECT?

I created a class, registered it (Q_DECLARE_METATYPE), and then push instances of it into a QVariantMap. If the object is a Q_GADGET my JS can read it perfectly, but when I switch it to a Q_OBJECT, the objects are blank.

I placed some code examples on SO here: http://stackoverflow.com/questions/43398740/qml-can-see-my-q-gadget-but-not-q-object

Basically I've narrowed it down to Q_GADGET vs Q_OBJECT, nothing else seems to make any difference. I'd prefer to use Q_OBJECT so that I can use signals and other objects in the QML/JS can bind to them.

Thanks.
Shawn Rutledge
2017-04-14 14:25:43 UTC
Permalink
You got an answer on StackOverflow already, but to repeat: he’s right, a QObject is always passed from C++ to QML by pointer, and a Q_GADGET is always passed by value. That means Q_GADGET is only suitable for smallish self-contained classes, because it will always be copied.
Post by Russell, Matthew
Why can my Q_GADGET be read perfectly in QML (JS) but not my Q_OBJECT?
I created a class, registered it (Q_DECLARE_METATYPE), and then push instances of it into a QVariantMap. If the object is a Q_GADGET my JS can read it perfectly, but when I switch it to a Q_OBJECT, the objects are blank.
I placed some code examples on SO here: http://stackoverflow.com/questions/43398740/qml-can-see-my-q-gadget-but-not-q-object
Basically I’ve narrowed it down to Q_GADGET vs Q_OBJECT, nothing else seems to make any difference. I’d prefer to use Q_OBJECT so that I can use signals and other objects in the QML/JS can bind to them.
Thanks.
_______________________________________________
Interest mailing list
http://lists.qt-project.org/mailman/listinfo/interest
Russell, Matthew
2017-04-17 13:04:25 UTC
Permalink
Awesome. That helps. I lost a lot of time mistakenly trying to "keep things simple" by using smart pointers or passing by value in the interim. But yeah, understanding this moving forward will really help.

Thanks

-----Original Message-----
From: Interest [mailto:interest-bounces+mrussell=***@qt-project.org] On Behalf Of Shawn Rutledge
Sent: Friday, April 14, 2017 10:26 AM
To: ***@qt-project.org
Subject: Re: [Interest] Q_GADGET vs Q_OBJECT

You got an answer on StackOverflow already, but to repeat: he’s right, a QObject is always passed from C++ to QML by pointer, and a Q_GADGET is always passed by value. That means Q_GADGET is only suitable for smallish self-contained classes, because it will always be copied.
Post by Russell, Matthew
Why can my Q_GADGET be read perfectly in QML (JS) but not my Q_OBJECT?
I created a class, registered it (Q_DECLARE_METATYPE), and then push instances of it into a QVariantMap. If the object is a Q_GADGET my JS can read it perfectly, but when I switch it to a Q_OBJECT, the objects are blank.
I placed some code examples on SO here: http://stackoverflow.com/questions/43398740/qml-can-see-my-q-gadget-but-not-q-object
Basically I’ve narrowed it down to Q_GADGET vs Q_OBJECT, nothing else seems to make any difference. I’d prefer to use Q_OBJECT so that I can use signals and other objects in the QML/JS can bind to them.
Thanks.
_______________________________________________
Interest mailing list
http://lists.qt-project.org/mailman/listinfo/interest
_______________________________________________
Interest mailing list
***@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest

Continue reading on narkive:
Loading...