035-Third generation software development-Qt property system

Header image

Third generation software development-Qt property system

Article directory

  • Third generation software development-Qt property system
    • Project Introduction
    • Qt property system
      • Purpose
      • Properties and class members
      • use
        • Requirements for declaring attributes
    • Dynamic properties
    • Properties and custom types
    • in conclusion


Keywords:
Qt
Qml
Q_PROPERTY ,
setProperty
Properties

Project introduction

Welcome to our QML & C++ project! This project combines the power of QML (Qt Meta-Object Language) and C++ to develop excellent user interfaces and high-performance backend logic.

In the project, we leveraged QML’s declarative syntax and visual design capabilities to create a modern user interface. Through intuitive coding and reusable components, we can quickly develop rich and diverse interface effects and animation effects. At the same time, we use QML’s powerful integration capabilities to easily integrate the underlying logic and data model of C++ into the front-end interface.

On the backend side, we use C++ to write high-performance algorithms, data processing, and computational logic. C++ is a powerful programming language that offers excellent performance and scalability. Our team is committed to optimizing code and reducing resource consumption to ensure that our projects run efficiently on a variety of platforms and devices.

Whether you’re interested in QML and C++ development or need us to build complex user interfaces and backend logic for you, we’re ready to support you. Please feel free to contact us and let’s build a modern, high-performance QML & C++ project together!

Important?

?The price of this column will increase after the third generation soft development update.

Qt property system

In fact, this is the first time that I have used the Qt property system in a large-scale system in a project. When it comes to the meta-object system, we all know that it is the foundation of Qt, but this Qt property system I don’t know how to describe it. The summary of my feeling after using it is that if it is a pure QWidget series code, it is better not to use it. If it is used together with Qml, it is really delicious.

Qt’s property system is a mechanism for defining, accessing, and managing properties in Qt objects. It provides a convenient way to define the properties of an object and allows access, modification, and monitoring by property name.

The following is a detailed introduction to Qt’s property system:

  1. Properties:

    • Properties are characteristics or states of a Qt object and can be of any data type, such as integers, strings, etc.
    • Properties can be defined in a class’s header file via the Q_PROPERTY macro.
  2. Accessor Functions:

    • For each property, Qt provides automatically generated accessor functions for getting and setting the property’s value.
    • Accessor functions follow the naming convention: propertyName() is used to get the property value, and setPropertyName(value) is used to set the property value.
  3. Reflection:

    • The Qt property system is based on the reflection mechanism, allowing the property information of an object to be obtained and manipulated at runtime.
    • You can use the QObject::metaObject() function to get the object’s metaobject (QMetaObject), and then use the metaobject’s methods to access properties.
  4. Meta-Object System:

    • The meta-object system is the core part of Qt and is used to support functions such as the property system, signals and slots.
    • QMetaObject is a meta-object that describes the structure and behavior of an object, including properties, methods, signals, slots, etc.
  5. Dynamic Properties:

    • Qt’s property system also supports dynamic properties, allowing new properties to be added to objects at runtime.
    • You can use the QObject::setProperty(propertyName, value) method to set dynamic properties, and use the QObject::property(propertyName) method to get dynamic property values.
  6. Signals and Slots:

    • The property system is closely related to the signals and slots mechanism. When the value of an attribute changes, signals and slots can be used to notify other parts to handle accordingly.
    • Notifications of property value changes can be triggered through the property’s notify signal.

Summary: Qt’s property system provides a convenient and powerful way to manage object properties. It supports static properties and dynamic properties, and is tightly integrated with the meta-object system, reflection, and signals and slots mechanisms, allowing developers to easily define, access, and manipulate object properties.

Purpose

The more I write about it, the more confused I become. As for Qt’s property system, I can only use it now, but I really want to know what the original intention of Qt was in designing this thing. Is it for the convenience of C++ and Qml interaction? I went to Baidu and Google with this question. After all, everyone knew exactly how to use it, but no one could tell me the origin, so the following is the answer given by GPT.

Q: What is the purpose of developing Qt’s property system?

Answer: The purpose of Qt’s property system is to provide a convenient and flexible way to manage object properties. It allows developers to define and manipulate object properties in Qt applications, including operations such as reading, writing, notifying, validating, and establishing relationships with other properties.

The following are some of the main purposes for developing Qt’s property system:

  1. Simplified property management: The Qt property system enables developers to manage an object’s properties in a consistent manner. By using the property system, you can integrate the reading, writing, and notification capabilities of properties into objects without having to write a lot of code manually.

  2. Support for meta-object systems: The property system is tightly integrated with Qt’s meta-object system. This means that properties can be accessed by name at runtime, and signals and slots are supported.

  3. Extensibility: The Qt property system is extensible and developers can define custom property types according to their needs. This enables developers to create flexible properties that suit specific application needs.

  4. Integration with interface frameworks: Qt’s property system integrates seamlessly with Qt’s interface frameworks such as Qt Widgets and Qt Quick. This means you can easily bind properties to user interface elements, enabling instant updates and interactions with property values.

Overall, the Qt property system is designed to help developers build Qt applications more efficiently by providing a simple, flexible, and extensible way to manage an object’s properties.

Attributes and class members

I specifically searched the help documentation for Q_PROPERTY, which describes it like this

This macro is used for declaring properties in classes that inherit QObject. Properties behave like class data members, but they have additional features accessible through the Meta-Object System.

This macro is used to declare properties in classes that inherit QObject. Properties behave like class data members, but they have additional functionality accessible through the meta-object system.

So I understand that I can treat attributes as class members, but I cannot treat class members as attributes. It can be understood this way.

Use

This is all the content on the Internet. Here we move the blog content of the big guys 一去、二三里

https://blog.csdn.net/liang19890820/article/details/52022714

? image-20230801225943282

Requirements for declaring attributes

To declare a property, use the Q_PROPERTY() macro in a class that inherits QObject.

Q_PROPERTY(type name
           (READ getFunction [WRITE setFunction] |
            MEMBER memberName [(READ getFunction | WRITE setFunction)])
           [RESET resetFunction]
           [NOTIFY notifySignal]
           [REVISION int]
           [DESIGNABLE bool]
           [SCRIPTABLE bool]
           [STORED bool]
           [USER bool]
           [CONSTANT]
           [FINAL])

The following example shows how to use the MEMBER keyword to export class member variables as Qt properties. NOTE: The NOTIFY signal must be specified in order to be used by QML. (I use the classic one)

 Q_PROPERTY(QColor color MEMBER m_color NOTIFY colorChanged)
    Q_PROPERTY(qreal spacing MEMBER m_spacing NOTIFY spacingChanged)
    Q_PROPERTY(QString text MEMBER m_text NOTIFY textChanged)
    ...
signals:
    void colorChanged();
    void spacingChanged();
    void textChanged(const QString & amp;newText);

private:
    QColor m_color;
    qreal m_spacing;
    QString m_text;

A property behaves like a data member of a class, but it has additional functionality accessed through the meta-object system.

If the MEMBER keyword is not specified, a READ access function is required. It is used to read property values. Ideally, a const function is used for this purpose, and it must return either the property type or a const reference. For example: QWidget::focus is a read-only property, accessed through the READ function QWidget::hasFocus().

A WRITE accessor is optional and is used to set the property’s value. It must return void and can only accept one parameter, and the type of the attribute is a type pointer or reference, for example: QWidget::enabled has the WRITE function QWidget::setEnabled(). Read-only properties do not require a WRITE function, for example: QWidget::focus does not have a WRITE function.

If the READ access function is not specified, MEMBER variable association is required. This makes a given member variable readable and writable without the need to create READ and WRITE access functions. If you need to control variable access, you can still use the READ and WRITE functions instead of just MEMBER (but not at the same time).

A RESET function is optional and is used to set the property to a context-specified default value. For example: QWidget::cursor has READ and WRITE functions QWidget::cursor() and QWidget::setCursor(), but also has a RESET function QWidget::unsetCursor(), because there is no available QWidget::setCursor() call to determine Resets the cursor attribute to the context default value. The RESET function must return void type and take no parameters.

A NOTIFY signal is optional. If NOTIFY is defined, you need to specify an existing signal in the class, which is emitted when the property value changes. The NOTIFY signal associated with the MEMBER variable must have zero or one argument and must be of the same type as the attribute. The parameter holds the new value of the attribute. The NOTIFY signal should only be emitted when the property value actually changes, to avoid being re-evaluated by QML. For example: Qt automatically emits a signal when a MEMBER property without an explicit setter is required.

A REVISION number is optional. If this keyword is included, it defines the property and notification signal used by a specific version of the API (usually QML); if not included, it defaults to 0.

The DESIGNABLE attribute specifies whether the property is visible in the editor in the GUI designer (for example: Qt Designer). Most properties are DESIGNABLE (default is true). In addition to true or false, you can also specify boolean member functions.

The SCRIPTABLE attribute indicates whether this property can be manipulated by a script engine (default is true). In addition to true or false, you can also specify boolean member functions.

The STORED attribute indicates whether the attribute exists independently or depends on other attributes. It also indicates whether the value of this property must be saved when saving the object state. Most properties are STORED (default true). But for example: STROED of QWidget::minmunWidth() is false because its value is obtained from the width part in QWidget::minimumSize() (type QSize).

The USER attribute specifies whether the property is designed to be visible and editable by the user. Normally, each class has only one USER attribute (default is false). For example: QAbstractButton::checked is a user-modifiable property of (checkable) buttons. Note: QItemDelegate gets and sets the USER property of the widget.

The presence of the CONSTANT attribute indicates that the attribute is a constant value. For a given object instance, the READ function of a constant property must return the same value each time it is called. The constant value may be different for different object instances. A constant property cannot have a WRITE function or NOYIFY signal.

The presence of the FINAL attribute indicates that the attribute cannot be overridden by derived classes. In some cases, this can be used for efficiency optimizations, but cannot be enforced by moc. Care must be taken not to override a FINAL attribute.

Dynamic attributes

I haven’t actually used this part yet, or I haven’t used it extensively, so I don’t have any feeling about it. Let’s move on to the content of the boss and the link above.

QObject::setProperty() can also be used to add new properties to an instance of a class at runtime. When it is called with a name and value, if a property with the specified name already exists in the QObject, and if the given value is compatible with the property’s type, then the value is stored in the property and true is returned. If the value is incompatible with the attribute type, the attribute’s value will not change and false will be returned. However, if a property with the specified name does not exist in QObject (for example, it is not declared with Q_PROPERTY()), a new property with the specified name and value is automatically added to QObject, but false will still be returned. This means that the return value cannot be used to determine whether a property has a value unless it is known in advance that the property already exists in the QObject.

Note: stateful properties are added to each instance, i.e. they are added to QObject, not QMetaObject. A property can be removed from an instance by passing the property name and an illegal QVariant value to QObject::setProperty(). The default QVariant constructor constructs an illegal QVariant.

Dynamic properties can be queried with QObject::property(), just like properties declared with Q_PROPERTY().

Attributes and custom types

Custom types used by properties need to be registered using the Q_DECLARE_METATYPE() macro so that their values can be stored in QVariant objects. This makes them suitable for static properties declared at class definition time using the Q_PROPERTY() macro, as well as dynamic properties created at runtime.

Summary

Well, to be honest, it’s still a bit unclear at the moment, and my ability can only reach this point. If I have a new understanding of the Qt property system in the future, we will write another article. This article will be like this first

Blog Signature 2021