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

Posted IT1995

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt文档阅读笔记-staticMetaObject解析与实例相关的知识,希望对你有一定的参考价值。

官方解析

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 · GitHubhttps://github.com/fengfanchen/Qt/tree/master/StaticMetaObjectDemo

以上是关于Qt文档阅读笔记-staticMetaObject解析与实例的主要内容,如果未能解决你的问题,请参考以下文章

Qt文档阅读笔记-Simple Chat Example解析

Qt文档阅读笔记-Qt5录音功能的实现

Qt文档阅读笔记-Broadcast Sender Example解析

Qt文档阅读笔记-QScopedPointer解析及实例

Qt文档阅读笔记-QNetworkProxy::ProxyType解析(Qt设置Fiddler代理)

Qt文档阅读笔记-QNetworkProxy::ProxyType解析(Qt设置Fiddler代理)