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. default property
QtWS25 Last Chance

default property

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qmlproperty
6 Posts 3 Posters 3.9k 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.
  • D Offline
    D Offline
    dream_captain
    wrote on 8 Nov 2017, 11:39 last edited by
    #1

    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?

    S 1 Reply Last reply 8 Nov 2017, 12:04
    1
    • D dream_captain
      8 Nov 2017, 11:39

      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?

      S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 8 Nov 2017, 12:04 last edited by
      #2

      @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(:^

      D 1 Reply Last reply 8 Nov 2017, 15:13
      1
      • S sierdzio
        8 Nov 2017, 12:04

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

        D Offline
        D Offline
        dream_captain
        wrote on 8 Nov 2017, 15:13 last edited by
        #3

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

        R 1 Reply Last reply 8 Nov 2017, 15:47
        0
        • D dream_captain
          8 Nov 2017, 15:13

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

          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 8 Nov 2017, 15:47 last edited by
          #4

          @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

          S 1 Reply Last reply 8 Nov 2017, 18:17
          1
          • R raven-worx
            8 Nov 2017, 15:47

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

            S Offline
            S Offline
            sierdzio
            Moderators
            wrote on 8 Nov 2017, 18:17 last edited by
            #5

            @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
            3
            • D Offline
              D Offline
              dream_captain
              wrote on 9 Nov 2017, 04:26 last edited by
              #6

              I get it. Thanks!

              1 Reply Last reply
              1

              1/6

              8 Nov 2017, 11:39

              • Login

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