[Interest] Setting propery values on Q_GADGET-based objects in qml possible?

Ola Røer Thorsen ola at silentwings.no
Tue Jun 20 15:06:00 CEST 2017

2017-06-20 13:52 GMT+02:00 Ola Røer Thorsen <ola at silentwings.no>:

> function doStuff() {
>    var o = theGadget;
>    console.log("o.name"); // prints "Name"
>    o.name = "Some other name"; // No error, but nothing really happens
> either
>    console.log("o.name"); // Still prints "Name" instead of "Some other
> name"
>    backend.setNewGadget(o);
> }
Turns out that the problem is that "o" in this case is not really my
MyGadget type, but rather a QVariant (?) containing the MyGadget type
because it was exposed to QML via a C++ list model (the model data()
function returns the gadget wrapped in QVariant).

If I make a hack addition to my class like
Q_INVOKABLE MyGadget getMyself() const { return *this; }

then call
var o = theGadget.getMyself();
o.name = ...

it actually works.

If any of you have some better way to do this I'd be happy to learn about
it. The QVariant::value function is not callable in qml.

Could the behavior with silently ignoring assigning property values when
wrapped in QVariant be considered a bug? If so I'll make a bug report.

Best regards,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20170620/99903a08/attachment.html>

More information about the Interest mailing list