学习 QT的 类反射机制
使用Qt 反射机制的条件
1.需要继承自QObject 类 或者 它的 派生类 ,并需要在类中加入Q_OBJECT 宏
2.注册成员函数:若希望普通成员函数能够被反射,需要在函数声明之前加入Q_INVOKABLE 宏。
3.注册成员变量:若希望成员变量能被反射,需要使用Q_PROPERTY 宏。
4.注册枚举变量:若希望枚举能被反射,需要使用Q_ENUM 或者 Q_FLAGS宏。
QMetaObjectTest.h
#pragma once
#include <QtWidgets/QWidget>
#include "ui_QMetaObjectTest.h"
#include <QMetaObject>
#include <QDebug>
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;
};
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;
}
main.cpp
#include "QMetaObjectTest.h"
#include <QtWidgets/QApplication>
#include <QMetaObject>
#include <QMetaMethod>
#include <QDebug>
#include <QMetaProperty>
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();
}
运行部分截图:
注册的部分信息
这是我们写的 返回类型 void 参数名称 a,b 必须要加Q_INVOKABLE
我们定义的 setName 和 getName;
这是我们类的 对象的属性 以及返回的类型
这是我们定义的 Q_PROPERTY(QString m_name READ getName WRITE setName)
中的 m_name
我们的枚举集合的解读 和 利用反射静态原对象 来 调用函数 必须加 Q_ENUM或者 Q_FLAGS
文章来源:https://www.toymoban.com/news/detail-483643.html
QT类反射机制一
QT类反射机制二
QT类反射机制三
QT类反射机制四文章来源地址https://www.toymoban.com/news/detail-483643.html
到了这里,关于QT笔记——QT类反射机制简单学习的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!