当前位置:网站首页>[QT notes] simple understanding of QT meta object system
[QT notes] simple understanding of QT meta object system
2022-06-27 06:09:00 【Lin Qi sevenlin】
Meta object system
QT The meta object system of provides :
- Signal and slot
- Runtime type information
- Dynamic attribute system
The meta object system is implemented based on the following three points :
- Use QObject As the base class
- Using macros in classes Q_OBJECT To turn on meta object properties , Such as : Dynamic properties 、 Signals and slots
- Using the meta object compiler (moc) For every one QObject Subclasses provide the necessary code to implement meta object features
The meta object compiler is compiling C++ Source code , If the source code contains Q_OBJECT macro , Then it will generate a new source code with meta object code information
Features provided by the meta object system
- Signal and slot
- QObject::metaObject() Return the meta object of the class
- QMetaObject::className() Returns the class name as a string at run time , Unwanted C++ Compiler vs native RTTI Support for
- QObject::inherits() Function returns whether the object is inherited QObject An instance of a class of a specified class in the inheritance tree .
- QObject::tr() and QObject::trUtf8() International translation string
- QObject::setProperty and QObject::property() Dynamically set and get properties according to their names
- QMetaObject::newInstance() Construct an instance of a new class
stay QObject Class , have access to qobject_cast() Perform dynamic conversion ,QT Medium qobject_cast() Similar to the standard C++ Medium dynamic_cast(), Its advantage is that no RTTI Support and work across dynamic library boundaries . If the type conversion succeeds , Returns a non-zero pointer ; If the conversion fails , Then return to nullptr
for instance :
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 Inherited from QObject
QTimer *timer = qobject_cast<QTimer *>(obj); // Type conversion succeeded
QPushButton *btn = qobject_cast<QPushButton *>(obj); // Type conversion failed ,QPushButton On QTimer incompatible types
// therefore ,btn == nullptr
Learning materials
- QT Help document
边栏推荐
- Code is data
- 项目-h5列表跳转详情,实现后退不刷新,修改数据则刷新的功能(记录滚动条)
- 力扣 179、最大数
- Built in functions of spark
- 【Cocos Creator 3.5.1】input.on的使用
- Crawler learning 5--- anti crawling identification picture verification code (ddddocr and pyteseract measured effect)
- 【Cocos Creator 3.5.1】坐标的加法
- 【Cocos Creator 3.5.1】this. node. Use of getposition (this.\u curpos)
- C# netcore中 配置帮助类IConfiguration
- 310. 最小高度树
猜你喜欢

Assembly language - Wang Shuang Chapter 13 int instruction - Notes

创建一个基础WDM驱动,并使用MFC调用驱动

POI 替换docx中的文字和图片

IAR systems fully supports Centrino technology 9 series chips

汇编语言-王爽 第11章 标志寄存器-笔记

美摄云服务方案:专为轻量化视频制作场景打造

The risk of multithreading -- thread safety

程序猿学习抖音短视频制作

Quick personal site building guide using WordPress

思维的技术:如何破解工作生活中的两难冲突?
随机推荐
IAR systems fully supports Centrino technology 9 series chips
资深【软件测试工程师】学习线路和必备知识点
代码即数据
[cocos creator 3.5.1] addition of coordinates
Information System Project Manager - Chapter VII project cost management
The risk of multithreading -- thread safety
下载cuda和cudnn
Inter thread wait and wake-up mechanism, singleton mode, blocking queue, timer
【QT小记】QT中正则表达式QRegularExpression的基本使用
表单校验 v-model 绑定的变量,校验失效的解决方案
Multithreading basic part2
机 器 学 习
【Cocos Creator 3.5.1】this. node. Use of getposition (this.\u curpos)
我对于测试团队建设的意见
openresty使用文档
【Cocos Creator 3.5.1】input.on的使用
How to check the frequency of memory and the number of memory slots in CPU-Z?
Altium designer 19 device silk screen label position shall be placed uniformly in batches
C语言练手小项目(巩固加深知识点理解)
信息系统项目管理师---第七章 项目成本管理