Andrey Pro
Andrey Pro

Reputation: 501

Questions about Qt properties

I would like to develop relatively simple dialog-based GUIs for Raspberry PI 2 (Linux/Raspbian) that is why would like to study Qt beginning with the basic/core features. RTFM, I know. So I read one of them, and to be honest understood very little.

  1. The biggest question in my head: Why would it have been worth reading in the first place? What is the purpose of the Qt properties? What are they good for?
  2. How do they work internally and from the point of view of writing the code.
  3. What is the memory and performance cost? Is it necessary to use them? If not, is it worth using them considering my use case?

Upvotes: 0

Views: 86

Answers (1)

Kevin Krammer
Kevin Krammer

Reputation: 5207

  1. Properties are used to read/write values in objects through a standard generic interface. Needed for interfacing with script engines (QtScript or QtQml), the widget designer, remote object interfaces (QtDBus, QtRemoteObjects, QtWebChannel).

  2. Most properties get implemented through normal getter/setter functions which are then tied to a property name and registered with the property system using the Q_PROPERTY macro. Alternatively the property name can be bound to a member variable. Read/write access with the generic property() and setProperty() API is the rerouted either to calls to the registered getter/setter or to the registered member variable.

  3. The property information is stored as a QMetaProperty in the class' staticMetaObject, access through the property API will incur a lookup based on the property name. Your use case doesn't seem to require usage of properties.

Another use case, as mentioned by Kuba in the comment, is to attach data to QObject based objects without modifying them.

These kind of properties, so called "dynamic properties", are handled slightly differently. Instead of getter/setter functions or a member variable, they are stored in a generic internal storage, currently a QVector<QVariant>

Upvotes: 2

Related Questions