QCoreApplication::aboutToQuit never emits
-
Just as title says application's signal for safe exit doesn't do anything. Want to mention that the project is a single Widget based application and that widget has WA_QuitOnClose and WA_DeleteOnClose attributes set to false. Here's a dedicated project code for this problem:
main.cpp
#include "widget.h" #include <QApplication> #include <QDebug> Widget* w; int main(int argc, char *argv[]) { QApplication a(argc, argv); w = new Widget; qDebug() << "start"; w->setAttribute(Qt::WA_QuitOnClose, false); w->setAttribute(Qt::WA_DeleteOnClose, false); a.connect( &a, &QCoreApplication::aboutToQuit, w, &Widget::quit); w->show(); return a.exec(); }widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> QT_BEGIN_NAMESPACE namespace Ui { class Widget; } QT_END_NAMESPACE class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = nullptr); ~Widget(); public slots: void quit(); private: Ui::Widget *ui; }; #endif // WIDGET_Hwidget.cpp
#include "widget.h" #include "./ui_widget.h" #include <QComboBox> #include <QString> #include <QTableWidget> #include <QDebug> Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); } Widget::~Widget() { delete ui; } void Widget::quit() { qDebug() << "quitting"; }Application's outputs
sigkill:
./build/Desktop-Debug/tsproblemtest start Quit (core dumped)sigterm:
$ ./build/Desktop-Debug/tsproblemtest start Terminatedsigquit:
$ ./build/Desktop-Debug/tsproblemtest start Quit (core dumped) -
Hi @pruf,
If I understand your code, and sample output, you seem to have the idea that Qt handles Unix signals, and exits gracefully on
SIGTERM... by default it does not. You can test yourQCoreApplication::aboutToQuit()signal/slot by, for example, setting up a timer to callQCoreApplication::quit()after a few seconds. But normally this would come from your UI widgets (eg a close button).If you really want to handle Unix signals (which I'm obligated say are not portable ;) then have a read of this: https://doc.qt.io/qt-6/unix-signals.html Especially see the
setup_unix_signal_handlers()function.Cheers.
-
Just as title says application's signal for safe exit doesn't do anything. Want to mention that the project is a single Widget based application and that widget has WA_QuitOnClose and WA_DeleteOnClose attributes set to false. Here's a dedicated project code for this problem:
main.cpp
#include "widget.h" #include <QApplication> #include <QDebug> Widget* w; int main(int argc, char *argv[]) { QApplication a(argc, argv); w = new Widget; qDebug() << "start"; w->setAttribute(Qt::WA_QuitOnClose, false); w->setAttribute(Qt::WA_DeleteOnClose, false); a.connect( &a, &QCoreApplication::aboutToQuit, w, &Widget::quit); w->show(); return a.exec(); }widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> QT_BEGIN_NAMESPACE namespace Ui { class Widget; } QT_END_NAMESPACE class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = nullptr); ~Widget(); public slots: void quit(); private: Ui::Widget *ui; }; #endif // WIDGET_Hwidget.cpp
#include "widget.h" #include "./ui_widget.h" #include <QComboBox> #include <QString> #include <QTableWidget> #include <QDebug> Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); } Widget::~Widget() { delete ui; } void Widget::quit() { qDebug() << "quitting"; }Application's outputs
sigkill:
./build/Desktop-Debug/tsproblemtest start Quit (core dumped)sigterm:
$ ./build/Desktop-Debug/tsproblemtest start Terminatedsigquit:
$ ./build/Desktop-Debug/tsproblemtest start Quit (core dumped)@pruf
I don't understand what you are doing or expecting at all. You show a program whose code should run fine. then, if I understand right, although you say nothing about sending the program Linux signals you seem to show you send it SIGKILL/TERM/QUIT signals (somehow)? And then it behaves just as you would expect an application to do under Linux when sent these signals, so what is the issue?If you are miraculously expecting Qt's
QCoreApplication::aboutToQuit()signal to be emitted on sending the program a Linux signal you will be disappointed. That is not it is there for at all. Rather it fires when the application is about to quit normally from the Qt event loop, e.g. when closing the last open window under default Qt behaviour.If you are wanting to handle Linux signals like
SIGTERM/SIGQUIT(you won't be able to handleSIGKILL) you need to do that by writing code to deal with Linuxsignal/sigset/sigaction()etc. quite separately from Qt code. -
Hi @pruf,
If I understand your code, and sample output, you seem to have the idea that Qt handles Unix signals, and exits gracefully on
SIGTERM... by default it does not. You can test yourQCoreApplication::aboutToQuit()signal/slot by, for example, setting up a timer to callQCoreApplication::quit()after a few seconds. But normally this would come from your UI widgets (eg a close button).If you really want to handle Unix signals (which I'm obligated say are not portable ;) then have a read of this: https://doc.qt.io/qt-6/unix-signals.html Especially see the
setup_unix_signal_handlers()function.Cheers.
-
P pruf has marked this topic as solved on