Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QCoreApplication::aboutToQuit never emits
Qt 6.11 is out! See what's new in the release blog

QCoreApplication::aboutToQuit never emits

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 961 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pruf
    wrote on last edited by
    #1

    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_H
    

    widget.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
    Terminated
    

    sigquit:

    $ ./build/Desktop-Debug/tsproblemtest
    start
    Quit (core dumped)
    
    JonBJ 1 Reply Last reply
    0
    • Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by
      #3

      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 your QCoreApplication::aboutToQuit() signal/slot by, for example, setting up a timer to call QCoreApplication::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.

      1 Reply Last reply
      3
      • P pruf

        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_H
        

        widget.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
        Terminated
        

        sigquit:

        $ ./build/Desktop-Debug/tsproblemtest
        start
        Quit (core dumped)
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #2

        @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 handle SIGKILL) you need to do that by writing code to deal with Linux signal/sigset/sigaction() etc. quite separately from Qt code.

        1 Reply Last reply
        2
        • Paul ColbyP Offline
          Paul ColbyP Offline
          Paul Colby
          wrote on last edited by
          #3

          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 your QCoreApplication::aboutToQuit() signal/slot by, for example, setting up a timer to call QCoreApplication::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.

          1 Reply Last reply
          3
          • P pruf has marked this topic as solved on

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved