注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

G G I C C I

 
 
 

日志

 
 

我的Qt学习笔记 1 从QObject开始  

2012-07-02 22:39:34|  分类: Qt |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

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, the destroyed() 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: }
    image


  • 如果一个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 the QObject directly if it exists in a different thread than the one currently executing. Use deleteLater() 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

多谢阅读,有误希望指正!

  评论这张
 
阅读(526)| 评论(0)
推荐

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017