Quit the application when QWidget is closed
-
@Knj-Tkm said in Quit the application when QWidget is closed:
How do I use it?
You need to override closeEvent in your class.
class MainWin: public QMainWindow { private: void closeEvent(QCloseEvent *event) override; } void MainWin::closeEvent(QCloseEvent *event) { qDebug() << "Closing"; event->accept(); }
Is MainWin a QMainWindow? I'm wondering why you need to close your app manually when main window is closed, this should happen automatically.
-
QWidget::showEvent() can be overridden to detect when a widget is hidden.QGuiApplication::setQuitOnLastWindowClosed() is likely what you're looking for.
-
@Knj-Tkm said in Quit the application when QWidget is closed:
But there is no signal when QWidget close.
But there is https://doc.qt.io/qt-5/qwidget.html#closeEvent
-
@Knj-Tkm said in Quit the application when QWidget is closed:
How do I use it?
You need to override closeEvent in your class.
class MainWin: public QMainWindow { private: void closeEvent(QCloseEvent *event) override; } void MainWin::closeEvent(QCloseEvent *event) { qDebug() << "Closing"; event->accept(); }
Is MainWin a QMainWindow? I'm wondering why you need to close your app manually when main window is closed, this should happen automatically.
-
@jsulm
It get error
[ error: only virtual member functions can be marked 'override']
Sorry, It work.
I got the code a little wrong.I use QWidget.
I'm creating an application window with QWidget.
I just want the application to quit when I close QWidget.