Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
435 views
in Technique[技术] by (71.8m points)

destructor - Are signals in Qt automatically disconnected when one of the class is deleted

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

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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.

Do take care though:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...