Abhi Garg
Abhi Garg

Reputation: 73

How can I create a QCloseEvent for a widget created by the mainwindow

My MainWindow creates a QWidget called wid. How can I create a slot that is activated when that widget is closed?

Upvotes: 0

Views: 298

Answers (1)

Mohit
Mohit

Reputation: 1305

You can subclass the QWidget and add the functionality as follows:

class CloseableWidget: public QWidget {
    Q_OBJECT
public:
    CloseableWidget(QWidget* parent = nullptr): QWidget(parent) {}
signals:
    void onClose(QCloseEvent* e);
protected:
    void closeEvent(QCloseEvent* e) override {
        emit onClose(e);
    }
};

and create instance of CloseableWidget instead of QWidget, Now you can connect your wid with onClose signal with a slot of MainWindow.

Upvotes: 1

Related Questions