0

This is my main.cpp which starts the mainwindow:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    TabWindow w;
    w.show();
    return a.exec();
}

Even with the a.connect(...) I do not understand why myApplication.exe still runs after I close the mainwindow. Any suggestions on how I can fully end all processes after the quit button is clicked?

EDIT: The Qt Documentation says this: We recommend that you connect clean-up code to the aboutToQuit() signal, instead of putting it in your application's main() function. This is because, on some platforms, the QApplication::exec() call may not return.

Dmitry Sazonov
  • 8,801
  • 1
  • 35
  • 61
BurninatorDor
  • 1,009
  • 5
  • 19
  • 42

4 Answers4

2

There's nothing wrong with your code. And your connect doesn't do anything.

Unless you call QGuiApplication::setQuitOnLastWindowClosed(true) somewhere, application should exit when the last window is closed. Probably you block event loop somewhere in your window code.

Oleg Shparber
  • 2,732
  • 1
  • 18
  • 19
1

Thanks to the comment posted by @ratchetfreak in my question, I figured out where the problem was.

In my MainWindow, I started a worker thread which was not terminated and thus still persisted as a process after the application was closed. In order to fix this, I registered the close event and kept track of the existence of the thread - i.e. basically, ignored the closeEvent until the thread was deleted as well.

void MainWindow::closeEvent(QCloseEvent *event)
{
    if (workerThreadExists) {
        // Gracefully exiting all tasks inside the worker thread
        while (workerThreadExists){
            event->ignore();
        }
        event->accept();

    }
}

...and for me workerThreadExists is just a BOOLEAN that is set to true once the thread is created and then it is set to false when the thread is deleted. Hope this helps!

BurninatorDor
  • 1,009
  • 5
  • 19
  • 42
0

You should have something like this:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    TabWindow w;
    w.show();
    return a.exec();
}

// Do quit in your TabWindow:
TabWindow::TabWindow()
  : public QWidget(NULL)
{
  connect( ui->myQuitButton, SIGNAL( clicked() ), qApp, SLOT( quit() ) );
}
Dmitry Sazonov
  • 8,801
  • 1
  • 35
  • 61
-5

just replace

return a.exec();

with

return 0;

That should end all processes which is associated with that program.

DarkEvE
  • 171
  • 12