Need something like a finished-signal from QWidget

21,327

Solution 1

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.

Solution 2

In your Widget class, you can add your own signal that others can connect to. Then override the closeEvent() method. Don't worry about overriding this method, this kind of situation is exactly the right reason do to it.

class MyCustomWidget: public QWidget
{
   Q_OBJECT

    ...

    signals:
       void WidgetClosed();

   protected:

     //===============================================================
     // Summary: Overrides the Widget close event
     //  Allows local processing before the window is allowed to close.
     //===============================================================
     void closeEvent(QCloseEvent *event);

    }

In the closeEvent method trigger your signal:

void MyCustomWidget::closeEvent(QCloseEvent *event)
{
      emit WidgetClosed();
      event->accept();
}
Share:
21,327
LarissaGodzilla
Author by

LarissaGodzilla

Updated on April 13, 2020

Comments

  • LarissaGodzilla
    LarissaGodzilla about 4 years

    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.

    • RedX
      RedX over 12 years
      Emit a signal from the widget? Or catch destroyed() signal?
    • Mat
      Mat over 12 years
      Your last line doesn't really make sense. A widget is a GUI item, its close event is GUI code. Why would overriding that cause a mix of GUI & business logic?
  • LarissaGodzilla
    LarissaGodzilla over 12 years
    I did that already, but for some reason that I might not have understood as well as I thought I´d have, my team-leader didn´t want it that way. But I´ll talk to him about that, seeing that other possible ways make much less sense.
  • SexyBeast
    SexyBeast over 9 years
    Hi Frank, how do I implement the widgetDestroyed slot?
  • Frank Osterfeld
    Frank Osterfeld over 9 years
    That depends on what you want to do when the widget is destroyed, there's no generic answer what to do in the slot.