Need something like a finished-signal from QWidget

后端 未结 3 751
猫巷女王i
猫巷女王i 2021-02-19 05:25

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 (whic

相关标签:
3条回答
  • 2021-02-19 05:46

    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();
    }
    
    0 讨论(0)
  • 2021-02-19 06:01

    QWidget doesn't have many signals really, according to the documentation it has a grand total of 2. However, that doesn't mean you can't specify a signal yourself and use it, which is probably the best method.

    0 讨论(0)
  • 2021-02-19 06:05

    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.

    0 讨论(0)
提交回复
热议问题