• QT笔记——QT类反射机制简单学习


    学习 QT的 类反射机制

    使用Qt 反射机制的条件

    1.需要继承自QObject 类 或者 它的 派生类 ,并需要在类中加入Q_OBJECT 宏
    2.注册成员函数:若希望普通成员函数能够被反射,需要在函数声明之前加入Q_INVOKABLE 宏。
    3.注册成员变量:若希望成员变量能被反射,需要使用Q_PROPERTY 宏。
    4.注册枚举变量:若希望枚举能被反射,需要使用Q_ENUM 或者 Q_FLAGS宏。

    在这里插入图片描述

    QMetaObjectTest.h

    #pragma once
    
    #include 
    #include "ui_QMetaObjectTest.h"
    #include 
    #include 
    
    class QMetaObjectTest : public QWidget
    {
        Q_OBJECT
        Q_CLASSINFO("author", "lion_cxq")   
        Q_PROPERTY(QString m_name READ getName WRITE setName)
    public:
         Q_INVOKABLE  QMetaObjectTest(QWidget *parent = Q_NULLPTR);
         enum PriorityType{ 
             High,
             Low, 
             VeryHigh,
             VeryLow 
         };
         Q_ENUM(PriorityType)
         Q_INVOKABLE void setName(QString name);
         Q_INVOKABLE QString getName();
         Q_INVOKABLE void testMessage(int a ,int b);
    private:
        Ui::QMetaObjectTestClass ui;
    	QString m_name;
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    QMetaObjectTest.cpp

    #include "QMetaObjectTest.h"
    
    QMetaObjectTest::QMetaObjectTest(QWidget *parent)
        : QWidget(parent)
    {
        ui.setupUi(this);
    }
    
    void QMetaObjectTest::testMessage(int a,int b)
    {
        qDebug() <<QStringLiteral("参数一:") << a<< QStringLiteral("参数二:") << b;
    }
    
    void QMetaObjectTest::setName(QString name)
    {
        m_name = name;
    }
    
    QString QMetaObjectTest::getName()
    {
        return m_name;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    main.cpp

    #include "QMetaObjectTest.h"
    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
       QMetaObjectTest object;
       const QMetaObject* theMetaObject = object.metaObject();
       
       //该类的类名
       qDebug() <<QStringLiteral("类名:") << theMetaObject->className();
    
       //该类注册的信息
       int  classInfoIndex;
       int classInfoCount = theMetaObject->classInfoCount();
       for (classInfoIndex = 0; classInfoIndex < classInfoCount; ++classInfoIndex)
       {
    	   QMetaClassInfo classInfo = theMetaObject->classInfo(classInfoIndex);
    	   qDebug() << "name: " << classInfo.name() << " " << "value: " << classInfo.value();
       }
       
    	//该类的成员
       int metathodIndex;
       int metathodCount = theMetaObject->methodCount();
       for (metathodIndex = 0; metathodIndex < metathodCount; ++metathodIndex)
       {
    	   QMetaMethod oneMethod = theMetaObject->method(metathodIndex);
    	   qDebug() << "typeName: " << oneMethod.typeName()<<" "<< "methodType: " << oneMethod.methodType()<<" "<<"parameterNames: " << oneMethod.parameterNames();
       }
    
       //该类的  属性
       int propertyIndex;
       int propertyCount = theMetaObject->propertyCount();
       for (propertyIndex = 0; propertyIndex < propertyCount; ++propertyIndex)
       {
    	   QMetaProperty oneProperty = theMetaObject->property(propertyIndex);
    	   qDebug() << "name: " << oneProperty.name()<<" "<<"type: " << oneProperty.type();
       }
    
       //该对象的  枚举集合
       int enumeratorIndex;
       int enumeratorCount = theMetaObject->enumeratorCount();
       for (enumeratorIndex = 0; enumeratorIndex < enumeratorCount; ++enumeratorIndex)
       {
    	   QMetaEnum oneEnumerator = theMetaObject->enumerator(enumeratorIndex);
    	   int enumIndex;
    	   int enumCount = oneEnumerator.keyCount();
    	   for (enumIndex = 0; enumIndex < enumCount; ++enumIndex)
    	   {
    		   qDebug() << oneEnumerator.key(enumIndex) << " ==> " << oneEnumerator.value(enumIndex);
    	   }
       }
    
       //获取该类的静态元对象  调用 并且实例化一个对象(我们并没有new它)
       QMetaObject metaObj = QMetaObjectTest::staticMetaObject;
       QObject* obj = metaObj.newInstance();
       QMetaObjectTest* metaObjectTest= qobject_cast<QMetaObjectTest*>(obj);
       metaObjectTest->testMessage(10, 20);
       
        return a.exec();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67

    运行部分截图:

    注册的部分信息
    在这里插入图片描述

    这是我们写的 返回类型 void 参数名称 a,b 必须要加Q_INVOKABLE
    在这里插入图片描述
    我们定义的 setName 和 getName;
    在这里插入图片描述

    这是我们类的 对象的属性 以及返回的类型
    在这里插入图片描述
    这是我们定义的 Q_PROPERTY(QString m_name READ getName WRITE setName)中的 m_name
    在这里插入图片描述

    我们的枚举集合的解读 和 利用反射静态原对象 来 调用函数 必须加 Q_ENUM或者 Q_FLAGS
    在这里插入图片描述

    QT类反射机制
    QT类反射机制二
    类反射机制说明

  • 相关阅读:
    【多线程】进程与线程 并发编程 面试题总结
    OpenCV官方教程中文版 —— 图像金字塔
    IIC/I2C总线实验
    Qt超时自动关闭子窗口
    第7章 【MySQL】B+树索引的使用
    SVM 用于将数据分类为两分类或多分类(Matlab代码实现)
    ubuntu虚拟机终端使用安装脚本报错,请问如何解决
    力扣 100. 相同的树 java
    某网吧网络布线规划设计
    Java内存区域速览
  • 原文地址:https://blog.csdn.net/lion_cxq/article/details/126091633