4

In my Qt app, I have a quit routine which gracefully cleans up everything before finally quitting, else there might be a crash somewhere. The app runs in the system tray, and there is a "Quit" menu defined for the system tray icon. On the quitAction I have set the menu role so that it's merged with Mac's app menu, but I don't see my slot being called. The code is below:

QAction *quitAction = new QAction(tr("&Quit"), this);
quitAction->setMenuRole(QAction::QuitRole);
connect(quitAction, SIGNAL(triggered()), this, SLOT(quittingApp()));

I have also tried to capture the QCloseEvent on QApplication but even that doesn't seem to work.

bool MyApplication::event(QEvent *ev)
{
    bool eaten = false;
    switch (ev->type())
    {
    case QEvent::Close:
    {
        quittingApp(); //My quit cleanup routine
        eaten = true;
        break;
    }
    default:
        eaten = QApplication::event(ev);
        break;
    }
    return eaten;
}

Am I missing something here? What's the best way to have my own cleanup routine which is called during quit?

Soumya Das
  • 1,635
  • 2
  • 19
  • 28

1 Answers1

-1

Connecting to the QCoreApplication::aboutToQuit signal is the best way to perform some last-second cleanup.

Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127
  • 1
    The event loop will be exited immediately after this signal is emitted. But in that case my clean up process won't really complete because it's a step by step cleaning process which needs the event loop to be running. – Soumya Das Nov 12 '13 at 10:11
  • 1
    @SoumyaDas: You can always spin a local event loop :) – Kuba hasn't forgotten Monica Nov 12 '13 at 21:06
  • 1
    Anyway, I don't even see the slot connected to QCoreApplication::aboutToQuit signal being called, so I really don't have any clue how to do my own cleanup when the user hits Cmd-Q to quit. – Soumya Das Nov 21 '13 at 07:19
  • 1
    This really doesn't answer the question. How does one **specifically** catch the menu Quit command to triage it - perhaps one should not be quitting or other things need taken care of, and so quit needs to be cancelled. One for instance is: catching menu quit as opposed to a subordinate window close button? – fyngyrz May 11 '18 at 14:01