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 has text property. I think it is because var type can be any other type and it will be checked when running the program. So if you don't give someText 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 the text 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 because someText is not in the visual parent hierarchy.

    Since MyLabel has its default property, so when Text{ text: "world" }is assigned to it, it does not set the visual parent of Text{ text: "world" }to MyLabel.( if MyLabel doesn't have default property, Text{ text: "world" } will be assigned to Item's data property, and this will set Text{ text: "world" }'s visual parent to MyLabel. )

    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!"

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.