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

  • Recently I was reading this article:

    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 ?

  • Lifetime Qt Champion


    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)

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

  • Lifetime Qt Champion

    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.

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

  • Lifetime Qt Champion

    You're welcome !

    Happy coding !

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.