删除其中一个类别时,Qt中的信号是否自动断开连接? [英] Are signals in Qt automatically disconnected when one of the class is deleted

查看:603
本文介绍了删除其中一个类别时,Qt中的信号是否自动断开连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当一侧之一被删除时,Qt是否自动删除对象之间的连接?

Does Qt automatically remove connections between objects , when one of the side is deleted ?

例如 connect(A ..,B .. ),当删除A(指针)或删除B时,连接会断开吗?

e.g connect (A .. , B ..) , when A (a pointer) is deleted , or B is deleted , will the connection be disconnected ?

是否有必要在析构函数中显式使用断开连接?

Is it necessary to use disconnect explicitly in destructor ?

推荐答案

是的, QObject ::〜QObject 析构函数负责解决此问题:

Yes, the QObject::~QObject destructor takes care of that:


所有往返对象的信号都会自动断开,并且

但是,使用 deleteLater() ,而不是直接删除QObject子类。

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可能导致崩溃。如果QObject与当前正在执行的线程不在同一线程中,则不能直接删除它。使用deleteLater()代替,这将导致事件循环在所有未决事件传递到对象后删除该对象。

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.

这篇关于删除其中一个类别时,Qt中的信号是否自动断开连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆