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

Tomasz Siekierda sierdzio at gmail.com
Wed Jun 28 10:34:14 CEST 2017


I've had similar problem and did not find a solution at the time.

While getMyself() feels hackish, it does indeed work. Thanks a lot for
sharing! :-)

On 20 June 2017 at 15:06, Ola Røer Thorsen <ola at silentwings.no> wrote:

>
> 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,
> Ola
>
>
>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20170628/b755b2a8/attachment.html>


More information about the Interest mailing list