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. Why use a QtObject to define properties rather than directly declaring properties?
Forum Updated to NodeBB v4.3 + New Features

Why use a QtObject to define properties rather than directly declaring properties?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 3 Posters 1.2k Views 1 Watching
  • 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
    Diracsbracket
    wrote on 4 Jun 2019, 02:55 last edited by
    #1

    Hello,

    The QtObject documentation shows the following example code:

    import QtQuick 2.0
    
    Item {
        QtObject {
            id: attributes
            property string name
            property int size
            property variant attributes
        }
    
        Text { text: attributes.name }
    }
    

    When/why use the above over directly declaring properties as shown below:

    Item {
        id: item
        property string name
        property int size
        property variant attributes
    
        Text { text: item.name }
    }
    

    Thanks~!

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 4 Jun 2019, 05:43 last edited by
      #2

      First, to make it clear - both approaches are good & correct.

      When/why use the above over directly declaring properties as shown below:

      I'll give you a few reasons, but as stated above - it's not in any way a requirement, just a "makes sense" recommendation:

      • if you embed QtObject inside your Item, the properties of QtObject will not be visible outside of Item. So it is a way of declaring private properties in QML. They are not really private... but kind of ;-)
      • since QtObject is lighter (less memory etc.) than an Item, in some cases it might be a good idea to use a QtObject as a container for frequently used properties, accessed by many components within a file or project (think of it as a "style definition" file, holding default margins, colours etc. in your app), especially if you also declare these properties as readonly

      (Z(:^

      D 1 Reply Last reply 4 Jun 2019, 09:43
      4
      • S sierdzio
        4 Jun 2019, 05:43

        First, to make it clear - both approaches are good & correct.

        When/why use the above over directly declaring properties as shown below:

        I'll give you a few reasons, but as stated above - it's not in any way a requirement, just a "makes sense" recommendation:

        • if you embed QtObject inside your Item, the properties of QtObject will not be visible outside of Item. So it is a way of declaring private properties in QML. They are not really private... but kind of ;-)
        • since QtObject is lighter (less memory etc.) than an Item, in some cases it might be a good idea to use a QtObject as a container for frequently used properties, accessed by many components within a file or project (think of it as a "style definition" file, holding default margins, colours etc. in your app), especially if you also declare these properties as readonly
        D Offline
        D Offline
        Diracsbracket
        wrote on 4 Jun 2019, 09:43 last edited by Diracsbracket 6 Apr 2019, 09:46
        #3

        Thanks, @sierdzio

        @sierdzio said in Why use a QtObject to define properties rather than directly declaring properties?:

        in some cases it might be a good idea to use a QtObject as a container for frequently used properties, accessed by many components within a file or project

        In that case, those properties could also simply be added to the top-level element, and accessed where needed, no? How does using a QtObject instead save any memory then?

        S 1 Reply Last reply 4 Jun 2019, 10:19
        0
        • D Diracsbracket
          4 Jun 2019, 09:43

          Thanks, @sierdzio

          @sierdzio said in Why use a QtObject to define properties rather than directly declaring properties?:

          in some cases it might be a good idea to use a QtObject as a container for frequently used properties, accessed by many components within a file or project

          In that case, those properties could also simply be added to the top-level element, and accessed where needed, no? How does using a QtObject instead save any memory then?

          S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 4 Jun 2019, 10:19 last edited by sierdzio 6 May 2019, 05:30
          #4

          @Diracsbracket said in Why use a QtObject to define properties rather than directly declaring properties?:

          Thanks, @sierdzio

          @sierdzio said in Why use a QtObject to define properties rather than directly declaring properties?:

          in some cases it might be a good idea to use a QtObject as a container for frequently used properties, accessed by many components within a file or project

          In that case, those properties could also simply be added to the top-level element, and accessed where needed, no?

          I meant creating a separate component out of it, shared between multiple files. Now that I think about it, though, I am not entirely sure it is allowed to have QtObject as a top-level component in a file.

          How does using a QtObject instead save any memory then?

          It doesn't, in the case you described. You're right.

          (Z(:^

          1 Reply Last reply
          0
          • F Offline
            F Offline
            fcarney
            wrote on 4 Jun 2019, 15:15 last edited by
            #5

            This article explains one use of QtObject in QML:
            https://qml.guide/using-the-qtobject-element/

            C++ is a perfectly valid school of magic.

            D 1 Reply Last reply 5 Jun 2019, 05:37
            1
            • F fcarney
              4 Jun 2019, 15:15

              This article explains one use of QtObject in QML:
              https://qml.guide/using-the-qtobject-element/

              D Offline
              D Offline
              Diracsbracket
              wrote on 5 Jun 2019, 05:37 last edited by Diracsbracket 6 May 2019, 05:43
              #6

              Thanks, @fcarney,
              The bit about the default property of the QtObject was interesting.

              @sierdzio said in Why use a QtObject to define properties rather than directly declaring properties?:

              Now that I think about it, though, I am not entirely sure it is allowed to have QtObject as a top-level component in a file.

              The example mentioned by @fcarney shows that it is possible indeed.

              What I will take away from this is that QtObject is mainly to keep certain things private by embedding them into a child object, and that it is more "lightweight" than an Item for achieving this.

              Cheers!

              1 Reply Last reply
              0

              1/6

              4 Jun 2019, 02:55

              • 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