In QT, is it safe to emit a signal from the closeEvent function?

Please consider the following code

class A : public QWidget
{
    signals:
       void readyToBeClosed();
    protected:
       void closeEvent(QCloseEvent* e)
       {
           QWidget::closeEvent(e);
           emit readyToBeClosed();
       }
}

class B: public QWidget
{
public:
   B(A* obj)
   {
       connect(obj,SIGNAL(readyToBeCLosed()),this,SLOT(cleanUp());
   }

private slots:
   void cleanUp()
   {
        //DO SOMETHING
   }
}

It is safe? Can I risk a failure because the obj object is destroyed after the emission signal and before the return of the cleanUp function?

Many thanks.

+3
source share
1 answer

This is because the signal / slot system works as function calls (with the addition of a “connection” mechanism), as indicated in the Qt Documentation on Signals and Slots .

, , , , , , . , closeEvent, , , , , .

, A . , ( vahancho).

: , , A cleanup. B A, , ( , , A , , , ). cleanup - A , :

, , , Qt::DirectConnection . , , , ( connect Qt::AutoConnection, , .. , )

Qt::QueuedConnection Qt::AutoConnection, undefined, , . A,

+3

All Articles