当前位置:网站首页>【QT小记】QT元对象系统简单认识
【QT小记】QT元对象系统简单认识
2022-06-27 05:46:00 【林柒Sevenlin】
元对象系统
QT的元对象系统提供了:
- 信号与槽
- 运行时类型信息
- 动态属性系统
元对象系统基于以下三点实现:
- 使用QObject作为基类
- 在类中使用宏Q_OBJECT来开启元对象特性,如:动态属性、信号和槽
- 使用元对象编译器(moc)为每一个QObject子类提供必要的代码来实现元对象特性
元对象编译器在编译C++源码时,如果在源码中检测到包含Q_OBJECT宏,则它会生成一份新的带有元对象代码信息的源码
元对象系统提供的特性
- 信号与槽
- QObject::metaObject()返回类的元对象
- QMetaObject::className()在运行时以字符串的形式返回类名,不需要C++编译器对原生RTTI的支持
- QObject::inherits()函数返回对象是否是继承 QObject 继承树中指定类的类的实例。
- QObject::tr()和QObject::trUtf8()国际化翻译字符串
- QObject::setProperty和QObject::property()根据属性名动态设置和获取属性
- QMetaObject::newInstance()构造一个新的类的实例
在QObject类上,可以使用qobject_cast()执行动态转换,QT中的qobject_cast()类似于标准C++中的dynamic_cast(),它的优点是不需要RTTI支持并且可以跨动态库边界工作。如果类型转换成功,则返回非零指针;如果转换失败,则返回nullptr
举个例子:
const QMetaObject *QObject::metaObject() const
const char *QMetaObject::className() const
QObject *obj = new QPushButton;
obj->metaObject()->className(); // returns "QPushButton"
QPushButton::staticMetaObject.className(); // returns "QPushButton"
bool QObject::inherits(const char *className) const
QTimer *timer = new QTimer; // QTimer inherits QObject
timer->inherits("QTimer"); // returns true
timer->inherits("QObject"); // returns true
timer->inherits("QAbstractButton"); // returns false
bool QObject::setProperty(const char *name, const QVariant &value)
QTimer *timer = new QTimer;
timer->setProperty("interval", 1000);
T qobject_cast(const QObject *object)
QObject *obj = new QTimer; // QTimer继承于QObject
QTimer *timer = qobject_cast<QTimer *>(obj); // 类型转换成功
QPushButton *btn = qobject_cast<QPushButton *>(obj); //类型转换失败,QPushButton于QTimer类型不兼容
// 所以,btn == nullptr
学习资料
- QT帮助文档
边栏推荐
- leetcode299周赛记录
- Open the door small example to learn ten use case diagrams
- Leetcode298 weekly race record
- Discussion on streaming media protocol (MPEG2-TS, RTSP, RTP, RTCP, SDP, RTMP, HLS, HDS, HSS, mpeg-dash)
- [FPGA] UART serial port_ V1.1
- C# netcore中 配置帮助类IConfiguration
- Luogu p2939 [usaco09feb]revamping trails G
- 开门小例子学习十种用例图
- 【FPGA】基于bt1120时序设计实现棋盘格横纵向灰阶图数据输出
- Add widget on qlistwidgetitem
猜你喜欢

开门小例子学习十种用例图

cpu-z中如何查看内存的频率和内存插槽的个数?

我对于测试团队建设的意见

C Primer Plus 第11章_字符串和字符串函数_代码和练习题

双位置继电器RXMD2-1MRK001984 DC220V

openresty使用文档

KubeSphere 集群配置 NFS 存储解决方案-收藏版

Two position relay hjws-9440
![[FPGA] realize the data output of checkerboard horizontal and vertical gray scale diagram based on bt1120 timing design](/img/80/c258817abd35887c0872a3286a821f.png)
[FPGA] realize the data output of checkerboard horizontal and vertical gray scale diagram based on bt1120 timing design

Codeforces Round #802 (Div. 2)
随机推荐
Add widget on qlistwidgetitem
Two position relay hjws-9440
Formation and release of function stack frame
Edge loads web pages in IE mode - edge sets ie compatibility
Two position relay rxmvb2 r251 204 110dc
Senior [Software Test Engineer] learning route and necessary knowledge points
【FPGA】UART串口_V1.1
Jump details of item -h5 list, and realize the function of not refreshing when backing up, and refreshing when modifying data (record scroll bar)
Go日志-Uber开源库zap使用
Opencv implements object tracking
STM32 MCU pin_ How to configure the pin of single chip microcomputer as pull-up input
leetcode298周赛记录
项目-h5列表跳转详情,实现后退不刷新,修改数据则刷新的功能(记录滚动条)
洛谷P2939 [USACO09FEB]Revamping Trails G 题解
Qt使用Valgrind分析内存泄漏
EasyExcel合并相同内容单元格及动态标题功能的实现
Win 10 如何打开环境变量窗口
【合辑】点云基础知识及点云催化剂软件功能介绍
three. JS first person camera gun following
C语言实现定时器