当前位置:网站首页>QT document reading notes staticmetaobject parsing and instances

QT document reading notes staticmetaobject parsing and instances

2022-06-21 23:30:00 IT1995

Official analysis

const QMetaObject *QObject::metaObject() const

Meta objects contain inheritance QObject Class ,QObject Subclasses of , Class name , Subclass name , attribute , Signals, slots, etc .

have access to metaObject The instance specifies the object , The following code :

QObject *obj = new QPushButton;
obj->metaObject()->className();             // returns "QPushButton"

QPushButton::staticMetaObject.className();  // returns "QPushButton"

Blogger examples

Here's a definition TestClass, Use Qt The reflex mechanism , Create a class , And set the value to print

Source code is as follows :

TestClass.h

#ifndef TESTCLASS_H
#define TESTCLASS_H

#include <QObject>
#include <QDebug>

class TestClass : public QObject
{
    Q_OBJECT
    Q_PROPERTY(int intValue READ getIntValue WRITE setIntValue)
    Q_PROPERTY(float floatValue READ getFloatValue WRITE setFloatValue)
    Q_PROPERTY(QString qstringValue READ getQStringValue WRITE setQStringValue)
    Q_PROPERTY(qlonglong qlonglongValue READ getQlonglongValue WRITE setQlonglongValue)
    Q_PROPERTY(QList<QVariant> qListValue READ getQListValue WRITE setQListValue)

public:
    Q_INVOKABLE TestClass(){}
    ~TestClass(){ qDebug() << "~TestClass() called"; }

    friend QDebug operator << (QDebug os, TestClass &testClass){

        os << "[";
        os << "(" << "intValue: " << testClass.getIntValue() << ", " <<
              "floatValue: " << testClass.getFloatValue() << ", " <<
              "QStringValue: " << testClass.getQStringValue() << ", " <<
              "qlonglongValue: " << testClass.getQlonglongValue() << ")";
        for(const QVariant &item : testClass.getQListValue()){

            os << "list: " << item;
        }
        os << "]";

        return os;
    }

    int getIntValue(){

        return this->intValue;
    }

    float getFloatValue(){

        return this->floatValue;
    }

    QString getQStringValue(){

        return this->qstringValue;
    }

    qlonglong getQlonglongValue(){

        return this->qlonglongValue;
    }

    QList<QVariant> getQListValue(){

        return this->qListValue;
    }

    void setIntValue(int value){

        this->intValue = value;
    }

    void setFloatValue(float value){

        this->floatValue = value;
    }

    void setQStringValue(QString value){

        this->qstringValue = value;
    }

    void setQlonglongValue(qlonglong value){

        this->qlonglongValue = value;
    }

    void setQListValue(QList<QVariant> value){

        qListValue.clear();
        for(const QVariant &item : value){

            this->qListValue.append(item);
        }
    }

private:
    int intValue;
    float floatValue;
    QString qstringValue;
    qlonglong qlonglongValue;
    QList<QVariant> qListValue;

};

#endif // TESTCLASS_H

main.cpp

#include "TestClass.h"
#include <QMetaObject>
#include <QMetaProperty>

int main(int argc, char *argv[])
{
    Q_UNUSED(argc)
    Q_UNUSED(argv)

    const QMetaObject *metaObject = &TestClass::staticMetaObject;
    QObject *obj = metaObject->newInstance();

    // get attribute 
    QStringList propertyList;

    for(int i = metaObject->propertyOffset(); i < metaObject->propertyCount(); i++){

        propertyList << metaObject->property(i).name();
    }

    // Set value by reflection 
    for(const QString &property : propertyList){

        if(property == "intValue"){

            obj->setProperty(property.toUtf8(), 10);
        }
        else if(property == "floatValue"){

            obj->setProperty(property.toUtf8(), 10.1f);
        }
        else if(property == "qstringValue"){

            obj->setProperty(property.toUtf8(), "HelloWorld");
        }
        else if(property == "qlonglongValue"){

            obj->setProperty(property.toUtf8(), 9999999999999);
        }
        else if(property == "qListValue"){

            QList<QVariant> list;
            list << "10086" << "10010" << "10000";
            obj->setProperty(property.toUtf8(), list);
        }
    }

    qDebug() << (*(TestClass*)obj);

    delete obj;

    return 0;
}

The screenshot of the program is as follows :

Source code package download address :

Qt/StaticMetaObjectDemo at master · fengfanchen/Qt · GitHubicon-default.png?t=M5H6https://github.com/fengfanchen/Qt/tree/master/StaticMetaObjectDemo

原网站

版权声明
本文为[IT1995]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206212138133027.html

随机推荐