Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved] What's the purpose of the Qt Property System ?
QtWS25 Last Chance

[solved] What's the purpose of the Qt Property System ?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 3.4k 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.
  • T3STYT Offline
    T3STYT Offline
    T3STY
    wrote on last edited by
    #1

    Recently I was reading this article:
    http://qt-project.org/doc/qt-5.0/qtcore/properties.html

    Qt provides this property system, based on Qt's Meta-Object System, which is said to be better than other property systems out there. But as far as I read the examples, it looks to me like there's no real advantage in using the property system rather than manually defining private variables in classes with setter and getter methods. For example, have a look at the "A simple Example" example code provided in the page I linked before. If I add a simple getPriority() method I could use the class without the Qt Property system. In fact, the only benefit I can see is that reading a property is as easy as reading a public variable in the class (so doing something like value = MyClass.priority); setting the priority value still requires to access a setter method (setPriority). My thought instead was that I could both read and write the properties using the same accessing syntax:
    @MyClass.property = some_value;
    read_value = MyClass.property;@
    but it's nothing like what I thought.

    So, what's the real purpose of Qt's Property System, and what are the advantages compared to private variables in classes with setter and getter methods ?

    1 Reply Last reply
    1
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      For example, you can for interact with various different objects by only analyzing what properties they have. Build application logic without knowing each and every class which is what you would need using only private variables with getter and setters.

      Take for example KDAB's gammaray.

      Or more closely, it's used in designer to setup the various properties of the widgets.

      It's also one of the base for QML to interact with C++. It also offers you for free compile time checking of your class interface. If you have properties it ensures that you have to provide what you wanted when you defined the property (getter/setter/signal etc…)

      This is not an exhaustive list of use (style sheets and more)

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • T3STYT Offline
        T3STYT Offline
        T3STY
        wrote on last edited by
        #3

        If I correctly understood, Qt's Property System is much more useful when dealing with the runtime environment rather than the coding environment. Programmatically (coding time) you still have to use the "standard" getters and setters for those properties, but at runtime you can have access to an object and only use its properties to manage it without the hassle of hard-coding an implementation; it would also explain why QML puts its base into it. Is that right?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          At programming time, you can use setProperty() if you don't want to use the setters. Having access to that doesn't mean that you should not use getters and setters anymore. It's an additional tool you can use.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • T3STYT Offline
            T3STYT Offline
            T3STY
            wrote on last edited by
            #5

            Thank you SGlast, I have a better idea on the property system now ;)

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              You're welcome !

              Happy coding !

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              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