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.
  • DiracsbracketD Offline
    DiracsbracketD Offline
    Diracsbracket
    wrote on 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
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on 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(:^

      DiracsbracketD 1 Reply Last reply
      4
      • sierdzioS sierdzio

        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
        DiracsbracketD Offline
        DiracsbracketD Offline
        Diracsbracket
        wrote on last edited by Diracsbracket
        #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?

        sierdzioS 1 Reply Last reply
        0
        • DiracsbracketD Diracsbracket

          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?

          sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by sierdzio
          #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
          • fcarneyF Offline
            fcarneyF Offline
            fcarney
            wrote on 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.

            DiracsbracketD 1 Reply Last reply
            1
            • fcarneyF fcarney

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

              DiracsbracketD Offline
              DiracsbracketD Offline
              Diracsbracket
              wrote on last edited by Diracsbracket
              #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

              • Login

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