default property
-
Hi, i'm new to QML and struggling to grasp the 'default property' concept.
What's the point of doing
// MyLabel.qml import QtQuick 2.0 Text { default property var someText text: "Hello, " + someText.text } ... MyLabel { Text { text: "world!" }}
instead of
// MyLabel.qml import QtQuick 2.0 Text { property string someText text: "Hello, " + someText } ... MyLabel { someText: "world" }
and why this snippet produce an error message "Unexpected object assignment"
// MyLabel.qml Text { default property string someText text: "Hello, " + someText } MyLabel { Text { text: "world!" } }
With an expression "Text { text: "world!" }" we are saying not to overwrite 'text' attribute?
-
@dream_captain said in default property:
and why this snippet produce an error message "Unexpected object assignment"
Because you are trying to assign a QML component Text to a string property. That won't work.
As for the general point of default properties: the example from the docs is perhaps a little bit hard to understand because it talks about Text and text and it's easy to confuse the string and the object.
Consider, instead, a Page component:
Page { Rectangle { //... } }
Thanks to default properties (mind you, I'm not sure if Page uses default property here, maybe it is some other magic - but it does not matter, let's assume it is a default prop), the Page component automatically recognizes Rectangle as it's "main" object. It will be added to the UI, stretched to fill the whole page automatically etc. And the code looks cleaner than:
Page { mainComponent: Rectangle { // ... } }
Because looking at the code you intuitively understand that Rectangle should be the main component.
-
@sierdzio Thanks for reply. I see the purpose of default property, but don't understand how it works. For example:
// MyLabel.qml import QtQuick 2.0 Text { default property var someText text: "Hello, " + someText.text } ... MyLabel { Text { text: "world!" }}
I can't inderstand how expression Text { text: "world!" } related to someText property. Is it some kind of strange binding inderneath the hood?
-
@dream_captain said in default property:
I can't inderstand how expression Text { text: "world!" } related to someText property. Is it some kind of strange binding inderneath the hood?
The default property tells where to ("fowardly") add the child elements, which are not assigned to a property but as a child to the object.
So in your example:
MyLabel { Text { text: "world!" } }
The
Text
child-element is put intoMyLabel
's default property. SosomeText.text
then is accessing thetext
property from the addedText
element. -
@raven-worx said in default property:
The Text child-element is put into MyLabel's default property. So someText.text then is accessing the text property from the added Text element.
In other words, you end up with TWO Text elements in this case: one is the MyLabel (which displays texts from both components), and another is your Text (which only provides the "world!" string).
-
I get it. Thanks!