Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Need help understanding default properties

Need help understanding default properties

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 3 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • DiracsbracketD Offline
    DiracsbracketD Offline
    Diracsbracket
    wrote on last edited by Diracsbracket
    #1

    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!

    FlotisableF johngodJ 2 Replies Last reply
    0
    • DiracsbracketD Diracsbracket

      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!

      FlotisableF Offline
      FlotisableF Offline
      Flotisable
      wrote on last edited by
      #2

      @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.

      DiracsbracketD 1 Reply Last reply
      0
      • FlotisableF Flotisable

        @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.

        DiracsbracketD Offline
        DiracsbracketD Offline
        Diracsbracket
        wrote on last edited by Diracsbracket
        #3

        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...

        FlotisableF 1 Reply Last reply
        0
        • DiracsbracketD Diracsbracket

          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...

          FlotisableF Offline
          FlotisableF Offline
          Flotisable
          wrote on last edited by Flotisable
          #4

          @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.

          1 Reply Last reply
          0
          • DiracsbracketD Diracsbracket

            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!

            johngodJ Offline
            johngodJ Offline
            johngod
            wrote on last edited by
            #5

            @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!"
            
            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved