最近研究了一下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的支持,提供類的附加信息
QMetaEnum
Qt特色的枚舉對象,支持key和 value之間的互轉
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關鍵字。
參考文章:
https://blog.csdn.net/playstudy/article/details/7861329
https://www.cnblogs.com/RainyBear/p/5251440.html
下面是我自己編寫的實例:
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
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;
}
#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;
}
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