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的支持,提供类的附加信息

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

相关推荐

  1. qt反射基础

    2023-12-08 01:30:04       32 阅读
  2. C# 反射基础

    2023-12-08 01:30:04       12 阅读
  3. WEB基础---反射

    2023-12-08 01:30:04       8 阅读
  4. <span style='color:red;'>反射</span>...

    反射...

    2023-12-08 01:30:04      7 阅读
  5. GO基础进阶篇 (十二)、反射

    2023-12-08 01:30:04       37 阅读
  6. Python基础20 面向对象(3)多态、封装、反射

    2023-12-08 01:30:04       30 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-08 01:30:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-08 01:30:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-08 01:30:04       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-08 01:30:04       20 阅读

热门阅读

  1. android 13.0 framework禁用系统所有通知

    2023-12-08 01:30:04       38 阅读
  2. Linux下超轻量级Rust开发环境搭建:一、安装Rust

    2023-12-08 01:30:04       38 阅读
  3. python pandas dataframe常用数据处理总结

    2023-12-08 01:30:04       37 阅读
  4. 纯C读取文件实现解析H264裸流每一帧数据

    2023-12-08 01:30:04       46 阅读
  5. Redisson

    2023-12-08 01:30:04       43 阅读
  6. 算法 拓扑序列

    2023-12-08 01:30:04       29 阅读
  7. Redis默认序列化方式乱码原因及解决办法

    2023-12-08 01:30:04       43 阅读
  8. 计算机网络——传输层

    2023-12-08 01:30:04       39 阅读
  9. python模块 — json

    2023-12-08 01:30:04       43 阅读
  10. TCP_NODELAY与TCP通信效率

    2023-12-08 01:30:04       43 阅读
  11. 【算法】合并K个升序链表

    2023-12-08 01:30:04       41 阅读
  12. Dynamo学习使用的网站

    2023-12-08 01:30:04       44 阅读
  13. 【NEON】学习资料汇总

    2023-12-08 01:30:04       44 阅读
  14. 【Centos8】配置网络镜像源

    2023-12-08 01:30:04       36 阅读