[Interest] getting type information of properties in QML

Mike Trahearn miketrahearn at imagineuisoftware.com
Wed Jul 17 11:58:44 CEST 2024


Hi Federico,

Glad it worked for you!

I suppose the only things to bear in mind are:

1) value types are always copied
2) each property doesn't have its own changed signal - Q_GADGETs can't have
them.

So even if you concoct something contrived like...

QtObject {
     id: otherObject
     property real redValue
     property real greenValue
     property real blueValue
}
property color myColor: {
    // yes, you can bind like this (but...)
    "r": otherObject.redValue,
    "g": otherObject.greenValue,
    "b": otherObject.blueValue
}
onMyColorChanged: {
    // ... then this will happen for
    // each individual r, g and b change and
    // you may get transient values of myColor.
}

Of course binding or assigning another color to myColor will do all three
in one hit (on the C++ side) using a copy constructor so you only get the
changed signal once in that case.

Ulf Hermann did a great presentation on this (value types - how they work
and how to make your own in C++) at QtWS23 but alas I can't find it on
YouTube right now.

All the best with your project!

On Wed, 17 July 2024, 6:58 pm Federico Ferri, <federico.ferri.it at gmail.com>
wrote:

> Excellent, thanks!
>
> *pragma ValueTypeBehavior: Addressable* does the trick.
>
> Is there any other pitfall besides the names of value types (color,
> vector3d, etc) shadowing any property with the same name?
>
> I wonder why this is not the default behavior.
>
> On Wed, 17 Jul 2024 at 02:05, Mike Trahearn <
> miketrahearn at imagineuisoftware.com> wrote:
>
>> Hello Federico,
>>
>> Here’s a little test I put together based on your description.
>>
>> 1) use* pragma ValueTypeBehavior: Addressable*
>> https://doc.qt.io/qt-6/qtqml-documents-structure.html#valuetypebehavior
>>
>> Among other things, this makes it possible to use value types with
>> *instanceOf*.
>>
>> 2) Value types can be assigned property-wise as shown below.
>>
>> 3) types like int are not value types so they have to be treated
>> differently in your case. However, Number.isInteger(n) may get you close.
>>
>> 4) So far as I can see, which types are value types and which are not is
>> not really explained in the following page (for which I have requested):
>>
>>
>> https://doc.qt.io/qt-6/qtqml-typesystem-valuetypes.html#built-in-value-types-provided-by-the-qml-language
>>
>> However if you go and find the definitions of these in the code, you can
>> see the properties you can assign for each of the value types:
>>
>>
>> https://code.qt.io/cgit/qt/qtdeclarative.git/tree/src/quick/util/qquickvaluetypes_p.h
>>
>> Hope this helps,
>>
>> Mike
>>
>>
>> [image: JS Type Test.png]
>>
>>
>> On 16 Jul 2024, at 8:10 PM, Federico Ferri <federico.ferri.it at gmail.com>
>> wrote:
>>
>> Is there any way to get type information about types of properties
>> defined in a QML Component?
>>
>> e.g.
>>
>> Item {
>>     property color c
>>     property vector3d v
>>     property int i
>>
>>     Component.onCompleted: {
>>         getPropertyType(c) === 'color'
>>         getPropertyType(v) === 'vector3d'
>>         getPropertyType(i) === 'int'
>>     }
>> }
>>
>> Details on my usecase: I receive messages over network that decode to JS
>> objects, but values such as colors and 3d vectors are encoded as simple JS
>> arrays (e.g. [0.0, 0.5, 1.0]) and direct assignment to the above properties
>> won't work.
>> _______________________________________________
>> Interest mailing list
>> Interest at qt-project.org
>> https://lists.qt-project.org/listinfo/interest
>>
>>
>> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> https://lists.qt-project.org/listinfo/interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20240717/77342fff/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: JS Type Test.png
Type: image/png
Size: 324615 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20240717/77342fff/attachment-0001.png>


More information about the Interest mailing list