Why use a QtObject to define properties rather than directly declaring properties?
-
Hello,
The QtObject documentation shows the following example code:
import QtQuick 2.0 Item { QtObject { id: attributes property string name property int size property variant attributes } Text { text: attributes.name } }
When/why use the above over directly declaring properties as shown below:
Item { id: item property string name property int size property variant attributes Text { text: item.name } }
Thanks~!
-
First, to make it clear - both approaches are good & correct.
When/why use the above over directly declaring properties as shown below:
I'll give you a few reasons, but as stated above - it's not in any way a requirement, just a "makes sense" recommendation:
- if you embed QtObject inside your Item, the properties of QtObject will not be visible outside of Item. So it is a way of declaring private properties in QML. They are not really private... but kind of ;-)
- since QtObject is lighter (less memory etc.) than an Item, in some cases it might be a good idea to use a QtObject as a container for frequently used properties, accessed by many components within a file or project (think of it as a "style definition" file, holding default margins, colours etc. in your app), especially if you also declare these properties as
readonly
-
Thanks, @sierdzio
@sierdzio said in Why use a QtObject to define properties rather than directly declaring properties?:
in some cases it might be a good idea to use a QtObject as a container for frequently used properties, accessed by many components within a file or project
In that case, those properties could also simply be added to the top-level element, and accessed where needed, no? How does using a
QtObject
instead save any memory then? -
@Diracsbracket said in Why use a QtObject to define properties rather than directly declaring properties?:
Thanks, @sierdzio
@sierdzio said in Why use a QtObject to define properties rather than directly declaring properties?:
in some cases it might be a good idea to use a QtObject as a container for frequently used properties, accessed by many components within a file or project
In that case, those properties could also simply be added to the top-level element, and accessed where needed, no?
I meant creating a separate component out of it, shared between multiple files. Now that I think about it, though, I am not entirely sure it is allowed to have QtObject as a top-level component in a file.
How does using a
QtObject
instead save any memory then?It doesn't, in the case you described. You're right.
-
This article explains one use of QtObject in QML:
https://qml.guide/using-the-qtobject-element/ -
Thanks, @fcarney,
The bit about the default property of theQtObject
was interesting.@sierdzio said in Why use a QtObject to define properties rather than directly declaring properties?:
Now that I think about it, though, I am not entirely sure it is allowed to have QtObject as a top-level component in a file.
The example mentioned by @fcarney shows that it is possible indeed.
What I will take away from this is that
QtObject
is mainly to keep certain things private by embedding them into a child object, and that it is more "lightweight" than anItem
for achieving this.Cheers!