我的Qt学习笔记 1 从QObject开始
来源:互联网 发布:网络银行系统架构 编辑:程序博客网 时间:2024/09/21 08:55
Title :
- QObject
Note 1 : 有关QObject的析构 QObject::~QObject () [virtual]
- QObject的析构会析构掉所有子对象(child objects,注意不是c++中的继承的概念,下面也是),有的时候delete不当会使程序崩溃,这里有关Qt的半自动化内存管理,介绍一篇前辈写的博客:http://blog.csdn.net/dbzhang800/article/details/6300025
- 析构对象后,所有与该对象的信号会被断开,所有与该对象有关的等待触发的事件会被从事件队列中移除。直接delete掉一个子对象往往没有调用deleteLater()函数安全。 (From QtDoc: All signals to and from the object are automatically disconnected, and any pending posted events for the object are removed from the event queue. However, it is often safer to use deleteLater() rather than deleting a QObject subclass directly. )
- 如果析构一个QObject对象的时候,它的子对象中有任何一个是在栈上建立或者是全局的对象的,那么会引起程序崩溃,代码示例如下。 ( From QtDoc:Warning: All child objects are deleted. If any of these objects are on the stack or global, sooner or later your program will crash. We do not recommend holding pointers to child objects from outside the parent. If you still do, thedestroyed() signal gives you an opportunity to detect when an object is destroyed. )
- destroyed()信号会在任何一个QObject对象被析构前发送 。
1: int main(int argc, char *argv[])
2: {
3: QObject *parent = new QObject;
4: QObject *child_1 = new QObject(parent);
5: QObject child_2(parent); //这是一个栈上的子对象
6: delete parent;
7: return 0;
8: }
- 如果一个QObject的对象的等待事件正在等待被传送,那么delete掉它会引起程序崩溃。所以不要直接delete掉一个运行在另一个线程中的QObject对象,用deleteLater()函数代替。 (From QtDoc:Warning: Deleting a QObject while pending events are waiting to be delivered can cause a crash. You must not delete theQObject directly if it exists in a different thread than the one currently executing. UsedeleteLater() instead, which will cause the event loop to delete the object after all pending events have been delivered to it.)
Note 2 : QObject 的一个属性 objectName
- 用setObjectName可以设置一个QObject对象的对象名,默认是空字符串,用findChild函数就可以定位设置了对象名的子对象啦,另外还有findChildren以及parent函数,功能如函数名。
1: int main(int argc, char *argv[])
2: {
3: QObject *parent = new QObject;
4: QObject *child_1 = new QObject(parent);
5: QObject *child_2 = new QObject(parent);
6: child_1->setObjectName("child_1");
7: cout << "child_1: " << child_1->objectName().toStdString() << endl;
8: cout << "child_2: " << child_2->objectName().toStdString() << endl << endl;
9: QObject* tmp = parent->findChild<QObject*>("child_1"); //findChild
10: cout << "child_1 addr: " << child_1 << endl;
11: cout << "tmp addr: " << tmp << endl;
12: delete parent;
13: return 0;
14: }
程序运行结果:
child_1: child_1
child_2:
child_1 addr: 0x2d18c0
tmp addr: 0x2d18c0
这是很基础的一点内容,其实QObject里面好一些有用的函数如:
- installEventFilter()
- connect()
- moveToThread()
- …
都将涉及更深层次的一些东西,如Qt中的事件驱动机制,以后在学习其它的一些类的时候肯定会涉及到QObject这儿的这些函数,所以先写到这儿,为以后的博客打个基础,晚安______
End:
Author : Ggicci
多谢阅读,有误希望指正!
0 0
- 我的Qt学习笔记 1 从QObject开始
- QT笔记:QObject::sender()的用法
- QT QObject::connect函数的学习
- QT学习:QObject::sender()的用法
- QT QObject::connect函数的学习
- QT QObject::connect函数的学习
- 开始我的学习笔记
- 从今天开始记录我的学习
- Qt学习笔记1(关于我的Qt)
- 从今天开始,这个blog就是我的笔记了,java学习笔记!wulllllllllllllllllllla!!!
- 准备从今天开始在这里记录我的学习ARM嵌入式的笔记了
- 从今天开始,从头开始学习Qt
- Qt源码学习之路(1) QObject类
- 从现在开始每天我要写学习笔记,希望成为自己一笔宝贵的财富!
- 嵌入式开发学习笔记(2)从单片机开始我的长线学习计划
- 2017.8.17 开始了我的QT 学习。
- QT QOBJECT
- Qt--QObject
- 使用IF NOT EXISTS创建数据表
- ETL利器Kettle实战应用解析系列一【Kettle使用介绍】
- Android开发之使用Intent传递对象
- 基于linux-2.6.35的class_create(),device_create解析
- C/C++命名规则
- 我的Qt学习笔记 1 从QObject开始
- LinkedIn高级分析师王益:大数据时代的理想主义和现实主义
- jQuery实现图片轮播效果(仿QQ商城首页、天猫首页)
- Hdu 4291
- c++11 Lambda
- 统计局人士:2014-2015年楼市见顶股市见底
- ubuntu 12.04 中eclipse下面提示框背景为黑色的解决方法
- 黑马程序员_总结笔记之IO流(1)_基本流
- linux下如何关闭防火墙?如何查看防火墙当前的状态