How to dynamically obtain the name and data of a class's fields in c++, qt

In C++ and Qt, the name and data of fields cannot be dynamically obtained directly through classes. C++ is a statically typed language, which needs to determine the structure of the class at compile time, including the names and data types of fields. Therefore, the field name needs to be referenced explicitly in the code to access its data.

However, you can use a reflection library or a custom meta-object system to achieve similar functionality. In Qt, Qt provides the Meta-Object System, which can dynamically obtain class property and method information at runtime. By using the meta-object system, you can get the property names of a class and access its data.

Here's an example of using Qt's meta-object system to get class field names and data:

#include <QMetaProperty>
#include <QDebug>

class MyClass : public QObject {
    
    
    Q_OBJECT
    Q_PROPERTY(int id READ getId WRITE setId)
    Q_PROPERTY(QString name READ getName WRITE setName)
    Q_PROPERTY(double value READ getValue WRITE setValue)

public:
    int id;
    QString name;
    double value;

    int getId() const {
    
     return id; }
    void setId(int value) {
    
     id = value; }

    QString getName() const {
    
     return name; }
    void setName(const QString& value) {
    
     name = value; }

    double getValue() const {
    
     return value; }
    void setValue(double value) {
    
     this->value = value; }
};

int main() {
    
    
    MyClass obj;
    obj.id = 1;
    obj.name = "Example";
    obj.value = 3.14159;

    const QMetaObject* metaObject = obj.metaObject();
    int propertyCount = metaObject->propertyCount();
    for (int i = 0; i < propertyCount; ++i) {
    
    
        const QMetaProperty property = metaObject->property(i);
        const char* name = property.name();
        QVariant value = property.read(&obj);
        qDebug() << "Field Name:" << name << "Value:" << value;
    }

    return 0;
}

In this example, MyClass is a class that inherits from QObject, using Qt's meta-object system. Three properties are defined by using the Q_PROPERTY macro: id, name and value. Each property provides methods for reading and writing.

In the main() function, a MyClass object obj is created and the values ​​of the properties are set. Then, get the metaObject of the object. Get the number of properties through metaObject->propertyCount() and use a loop to iterate through all properties. Use property.name() to get the name of a property, and property.read(&obj) to read the value of a property.

Running the above example will output:

Field Name: "id" Value: QVariant(int, 1)
Field Name: "name" Value: QVariant(QString, "Example")
Field Name: "value" Value: QVariant(double, 3.14159)

Note that the field names in this example are obtained through the meta-object system, not directly from the class definition. This method relies on Qt's meta-object system and can only obtain properties defined through Q_PROPERTY.

Guess you like

Origin blog.csdn.net/zhangzhechun/article/details/132384124