Qtのリフレクションメカニズムを使用して、記述されたクラスの関連情報(クラスメンバー関数の数、タイプ、名前など、サンプルコード付き)を取得します。

私は最近、Qtの反射メカニズムを研究し
ました。Qtのメタオブジェクトシステムによって提供される信号/スロットメカニズムの特性に加えて、次の特性も提供します。

QObject :: metaObject()
は、関連付けられたメタオブジェクトを返します

QMetaObject :: className()
は、実行時の状態でクラス名を返します

QObject :: inherits()
は、クラスの継承関係を決定します

QObject :: tr()、QObject :: trUtf8()
は、国際化、翻訳文字列を提供します

QObject :: setProperty()、QObject :: property()
は、名前でプロパティを動的に設定および取得します

QMetaObject :: newInstance()
は新しいインスタンスを作成します

QObject :: metaObject()メソッドを介して、QObjectから継承するすべてのクラスは、メタオブジェクトシステムによって生成されたmetaObjectオブジェクトを返すことができます。QMetaObjectによって提供されるいくつかの重要な情報:

QMetaClassInfo
は、マクロQ_CLASSINFOのサポートを通じて、クラスに関する追加情報を提供します。


キーと値の間の相互変換をサポートするQMetaEnumQtの特徴的な列挙オブジェクト

QMetaMethod
は、クラスメンバー関数のメタデータを提供します

QMetaProperty
は、クラスメンバープロパティのメタデータを提供します

Qt反射前期准备
1、首先得继承于Q_Object,同时需要在class中加入Q_OBJECT。
2、注册类成员变量需要使用Q_PROPERTY
Q_PROPERTY( type member READ get WRITE set) 其中READ,WRITE是关键字
Type表示成员的类型(不支持自定义类型,对Qt很多基本类型都支持);
Member代表你给该成员另外起的名字,可以和变量名不同;get,set就是自己在C++函数里面定义的基本的访问函数名,不需要写参数。

3、注册类成员函数
如果你希望这个函数能够被反射,那么很简单,只需要在类的函数声明前加入Q_INVOKABLE关键字。
参考文章:
blog.csdn.net/playstudy/a…
www.cnblogs.com/RainyBear/p…

下面是我自己编写的实例:
1.右击QT Creater中的项目名——添加新文件——选择C++ Class——Choose——取个Class name:TestClass——Base Class选择QObject——点击下一步——在项目文件列表中会增加一个testclass.h和testclass.cpp
testclass.h中如下:

#ifndef TESTCLASS_H
#define TESTCLASS_H
#include <QObject>
class TestClass : public QObject
{
    Q_OBJECT
public:
    explicit TestClass(QObject *parent = 0);
    Q_INVOKABLE int sum(int na,int nb);
    Q_INVOKABLE int decrease(int na, int nb);
signals:
public slots:
};
#endif // TESTCLASS_H
复制代码

testclass.cpp中如下:

#include "testclass.h"
    TestClass::TestClass(QObject *parent) : QObject(parent)
    {
    
    }
    int TestClass::sum(int na,int nb)
    {
        return na+nb;
    }
    int TestClass::decrease(int na, int nb)
    {
        return na-nb;
    }
复制代码

在主界面的cpp中添加一个按钮响应函数,如下:
注:在主界面.cpp中要包含TestClass的头文件#include “testClass.h”

void MainWindow::on_ShowClassInfo_clicked()
{
    TestClass classTestClass;
    const QMetaObject *theMetaObject = classTestClass.metaObject();//定义一个QMetaObject对象指针,用来获取类classTestClass的相关信息
    int nMetathodCount = theMetaObject->methodCount();
    for(int nMetathodIndex = 0;nMetathodIndex < nMetathodCount;nMetathodIndex++)
    {
        QMetaMethod oneMethod = theMetaObject->method(nMetathodIndex);
        qDebug() <<"MethodName: " <<oneMethod.name();
        qDebug() <<"parameterNames: " <<oneMethod.parameterNames();
        qDebug()<<"parameterTypes" << oneMethod.parameterTypes();
        qDebug() <<"typeName: " <<oneMethod.typeName();
        qDebug() <<"signature: " <<oneMethod.Signal;
        qDebug() <<"methodType: " <<oneMethod.methodType() <<"\n";
    }
}
复制代码

在这里插入图片描述
程序运行后,点击ShowClassInfo按钮,在“应用程序输出”界面会显示如下信息:

MethodName:  "destroyed"
parameterNames:  ("")
parameterTypes ("QObject*")
typeName:  void
signature:  1
methodType:  1 

MethodName:  "destroyed"
parameterNames:  ()
parameterTypes ()
typeName:  void
signature:  1
methodType:  1 

MethodName:  "objectNameChanged"
parameterNames:  ("objectName")
parameterTypes ("QString")
typeName:  void
signature:  1
methodType:  1 

MethodName:  "deleteLater"
parameterNames:  ()
parameterTypes ()
typeName:  void
signature:  1
methodType:  2 

MethodName:  "_q_reregisterTimers"
parameterNames:  ("")
parameterTypes ("void*")
typeName:  void
signature:  1
methodType:  2 

MethodName:  "sum"
parameterNames:  ("na", "nb")
parameterTypes ("int", "int")
typeName:  int
signature:  1
methodType:  0 

MethodName:  "decrease"
parameterNames:  ("na", "nb")
parameterTypes ("int", "int")
typeName:  int
signature:  1
methodType:  0 
复制代码

可以看到,TestClass实例化的类变量classTestClass的QMetaObject遍历出来的QMetaMethod一共有7个,其中除了我们手动添加的int sum(int na,int nb);和int decrease(int na, int nb);之外,还有5个自动生成的隐藏的QMetaMethod。

おすすめ

転載: juejin.im/post/7022845686752378916