Need something like a finished-signal from QWidget

LarissaGodzilla picture LarissaGodzilla · Oct 28, 2011 · Viewed 19.5k times · Source

I'm searching for something like the finished-signal from QDialog, only for QWidget. The reason is, I disable my toolbar once the widget pops up (which isn't a problem at all) and I want the toolbar to be enabled again, once the widget is closed.

I also can't override the close-Event of that widget, because then we would have GUI-code in business-classes.

Answer

Frank Osterfeld picture Frank Osterfeld · Oct 30, 2011

You can set the widget to be deleted on close, and then listen to its destroyed signal:

widget->setAttribute( Qt::WA_DeleteOnClose );
connect( widget, SIGNAL(destroyed(QObject*)), this, SLOT(widgetDestroyed(QObject*)) );

That only works if you're not interested in the widget contents though. At the point destroyed() is emitted, the widget isn't a QWidget anymore, just a QObject (as destroyed() is emitted from ~QObject), so you can't cast the argument QObject* to QWidget anymore.

A simple alternative might be to wrap your widget with a QDialog.