Unsolved Need help understanding default properties
-
Hi.
I've been trying to understand the explanation of default properties as given in the Qt documentation here.A default property is the property to which a value is assigned if an object is declared within another object's definition without declaring it as a value for a particular property.
The example given is a type definition in MyLabel.qml:
// MyLabel.qml import QtQuick 2.0 Text { default property var someText text: "Hello, " + someText.text }
Then, MyLabel is used somewhere else as follows:
MyLabel { Text { text: "world!" } }
which is equivalent to:
MyLabel { someText: Text { text: "world!" } }
This gives indeed the result "Hello world!"
But what is going on here? Why does:
MyLabel { Text { text: "world!" } }
not directly set the text property of the Text object in MyLabel, as in a normal property value assignment (which would result in the output "world!") ? Why is the property binding as defined in the initialization not overridden?
Why then does the following give output "world!"? :
MyLabel{ text: "world!" }
Why does the var someText have a .text field? i.e. why is it of the Text type (as suggested by the second usage syntax) ?
Finally, why does the following:
MyLabel.text: "world!"
result in error "Non-existent attached object" ?
Thanks for helping me see the light here...
Cheers! -
@Diracsbracket
though I just read the reference after reading your question, this is my opinion:in
MyLabel { Text { text: "world!" } }
it does not specify which property to be set. In this condition, the default property which is
someText
will be set.in
MyLabel{ text: "world!" }
the
text
property is specified, so the origin value"Hello, " + someText.text
is replaced by"world!"
as for the reason that
someText
hastext
property. I think it is becausevar
type can be any other type and it will be checked when running the program. So if you don't givesomeText
the right value, maybe you will get an error.and for the last question. I think that it is because
MyLabel
is a type, not an instance. So trying to get thetext
property will get error. -
Hi @Flotisable
Thanks for your kind reaction. There's so much to take in, but luckily the Qt documentation is excellent usually, and then there is this forum...So the someText in the parent Text object in MyLabel.qml gets assigned an object which is also of type Text in the example above,
so we have a Text within a Text. Why aren't both Text objects (the parent and the child) rendered then? Why is only the text from the parent
visible? Both the parent and the child are visual items derived from the Item type, so why is only the parent's text showing?Thanks gain, and In the meanwhile, I'll continue reading the online doc...
-
@Diracsbracket
after read some document, I think that it is becausesomeText
is not in the visual parent hierarchy.Since
MyLabel
has its default property, so whenText{ text: "world" }
is assigned to it, it does not set the visual parent ofText{ text: "world" }
toMyLabel
.( ifMyLabel
doesn't have default property,Text{ text: "world" }
will be assigned toItem
'sdata
property, and this will setText{ text: "world" }
's visual parent toMyLabel
. )I think you can read the Visual Parent document.
-
@Diracsbracket said in Need help understanding default properties:
Finally, why does the following:
MyLabel.text: "world!"result in error "Non-existent attached object" ?
I think this is because you have to set an id to MyLabel so you can set a binding to text
MyLabel { id: myID } ...... myID.text: "world!"