当前位置: 首页 > news >正文

qt反射之类反射、方法反射、字段反射

话不多说,直接上代码:
main.cpp:

#include < QCoreApplication >
#include “fstudent.h”
#include “manage.h”
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//注册类型
qRegisterMetaType(“FStudent”);

Manage manage;
manage.print();return a.exec();

}

反射类头文件:fstudent.h
#ifndef FSTUDENT_H
#define FSTUDENT_H
/*
1、这个类必须直接或间接继承自QObject
2、这个类必须写上Q_OBJECT宏定义
3、需要将类注册到元对象系统中
4、构造函数必须声明可调用
*/
#include
#include
#include
class FStudent : public QObject
{
//元对象系统必须添加 Q_OBJECT 引入元对象
Q_OBJECT
//实现属性m_Name反射,需要下面声明:
Q_PROPERTY(QString m_Name MEMBER m_Name)
public:
//构造函数必须用Q_INVOKABLE 进行修饰,不然该类无法做到反射
Q_INVOKABLE explicit FStudent(QObject *parent=nullptr);

~FStudent();
//该方法如果要做到反射,以下任一一个可以做到
//1、 添加 Q_INVOKABLE修饰
//2、 加slots
//3、加signals
Q_INVOKABLE void print();

public slots:

private:

QString m_Name;

};
Q_DECLARE_METATYPE(FStudent)

#endif // FSTUDENT_H

反射类源文件:fstudent.cpp

#include “fstudent.h”

#include

FStudent::FStudent(QObject *parent):QObject(parent)
{

}

FStudent::~FStudent()
{

}

void FStudent::print()
{
qDebug() << this->m_Name;
}

管理反射类头文件:manage.h

#ifndef MANAGE_H
#define MANAGE_H

#include
#include
class Manage
{
public:
Manage();
QList<QObject*> stu_obj_list;
void print();
};

#endif // MANAGE_H

管理反射类源文件:manage.cpp

#include “manage.h”
#include
#include
#include
#include
#include
#include
Manage::Manage()
{
//QT6.5.3下可用,在qt5.12.10下不可用
const QMetaObject* MetaObject = QMetaType::fromName(“FStudent”).metaObject();

//以下在qt5.12.10下不可用
//int typenumber = QMetaType::type(“FStudent”);

//const QMetaObject* MetaObject = QMetaType::metaObjectForType(typenumber);if(MetaObject == nullptr)
{qDebug() << "get FStudent MetaObject failed!!!";return;
}QStringList stu_name_list;
stu_name_list << "张三" << "李四" <<"王二" <<"赵六";for(auto& stu_name:stu_name_list)
{QObject* stu_obj = MetaObject->newInstance();stu_obj->setProperty("m_Name",QVariant(stu_name));stu_obj_list.push_back(stu_obj);
}

}

void Manage::print()
{
for(auto& stu_obj:stu_obj_list)
{
//通过反射属性来打印信息
qDebug() << stu_obj->property(“m_Name”).toString();
//通过反射方法来打印信息
QMetaObject::invokeMethod(stu_obj,“print”);

}
}

http://www.lryc.cn/news/425966.html

相关文章:

  • 服务器数据恢复—raid5阵列离线硬盘强制上线失败如何恢复数据?
  • FastAPI+Vue3零基础开发ERP系统项目实战课 20240815上课笔记 列表和字典相关方法的学习和练习
  • 基于微信小程序的诗词智能学习系统的设计与实现(全网独一无二,24年最新定做)
  • httplib库:用C++11搭建轻量级HTTP服务器
  • 基于嵌入式C++、SQLite、MQTT、Modbus和Web技术的工业物联网网关:从边缘计算到云端集成的全栈解决方案设计与实现
  • Chapter 38 设计模式
  • Redis5主备安装-Redis
  • C++票据查验、票据ocr、文字识别
  • pytest.ini介绍
  • Vue项目打包成桌面应用
  • DEFAULT_JOURNAL_IOPRIO
  • 【阿卡迈防护分析】Vueling航空Akamai破盾实战
  • 使用AWS Lambda轻松开启Amazon Rekognition之旅
  • 如何获取VS Code扩展的版本更新信息
  • Python开源项目周排行 2024年第13周
  • day04--js的综合案例
  • 【产品经理】定价策略
  • webrtc学习笔记3
  • Transformer架构;Encoder-Decoder;Padding Mask;Sequence Mask;
  • 【leetcode详解】特殊数组II : 一题代表了一类问题(前缀和思想)
  • SQL每日一练-0814
  • Android持久化技术—文件存储
  • 动手学深度学习(pytorch)学习记录12-激活函数[学习记录]
  • 微服务实战系列之玩转Docker(十)
  • Mysql(四)---增删查改(进阶)
  • SOAP @WebService WSDL
  • 【Qt】QWidget的toolTip属性
  • 【操作系统】什么是进程?什么是线程?两者有什么区别(面试常考!!!)
  • AI -- Machine Learning
  • 了解交换机_1.交换机的技术发展