Qt文档阅读笔记-staticMetaObject解析与实例

x33g5p2x  于2022-06-24 转载在 其他  
字(3.5k)|赞(0)|评价(0)|浏览(457)

官方解析

const QMetaObject *QObject::metaObject() const

元对象包含继承QObject的类,QObject的子类,类名,子类名,属性,信号和槽等等等。

可以使用metaObject实例会指定对象,如下代码:

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

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

博主例子

这里定义了一个TestClass,使用Qt反射机制,创建类,并设置值打印

源码如下:

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();

    //获取属性
    QStringList propertyList;

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

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

    //通过反射设置值
    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;
}

程序运行截图如下:

源码打包下载地址:

Qt/StaticMetaObjectDemo at master · fengfanchen/Qt · GitHub

https://github.com/fengfanchen/Qt/tree/master/StaticMetaObjectDemo

相关文章