Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved default property

    QML and Qt Quick
    qml property
    3
    6
    2988
    Loading More Posts
    • 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.
    • dream_captain
      dream_captain last edited by

      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?

      sierdzio 1 Reply Last reply Reply Quote 1
      • sierdzio
        sierdzio Moderators @dream_captain last edited by

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

        (Z(:^

        dream_captain 1 Reply Last reply Reply Quote 1
        • dream_captain
          dream_captain @sierdzio last edited by

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

          raven-worx 1 Reply Last reply Reply Quote 0
          • raven-worx
            raven-worx Moderators @dream_captain last edited by

            @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 into MyLabel's default property. So someText.text then is accessing the text property from the added Text element.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            sierdzio 1 Reply Last reply Reply Quote 1
            • sierdzio
              sierdzio Moderators @raven-worx last edited by

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

              (Z(:^

              1 Reply Last reply Reply Quote 3
              • dream_captain
                dream_captain last edited by

                I get it. Thanks!

                1 Reply Last reply Reply Quote 1
                • First post
                  Last post