Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close correctly a Qt program?

Tags:

c++

qt

When I try to close my Qt program, it just keeps running in the background though there's no window anymore.

Basically, I would like to know what I should do so it closes properly when I click the red cross on my main window (which has no parent).

Following this link, I tried a few things like :

QApplication app(argc, argv);
//...
app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
return app.exec();

or

QApplication app(argc, argv);
//...
app.setQuitOnLastWindowClosed(true);
return app.exec();

but neither work, the process still stays in memory, after the cross is clicked.

Then, how can I close correctly my program ?

like image 559
JBL Avatar asked Jun 12 '13 09:06

JBL


1 Answers

You can close your application manually using QApplication::quit().

By default the execution is terminated when the last top level window with the Qt::WA_QuitOnClose attribute has been closed. You don't need to connect lastWindowClosed to quit because it repeats the default setQuitOnLastWindowClosed behavior. You don't need to do setQuitOnLastWindowClosed(true) either because it's true by default. The code you've posted does nothing (if nothing is changed somewhere else).

Possible solutions:

  • Check your main window attributes. Maybe you have removed Qt::WA_QuitOnClose attribute.
  • If you have reimplemented closeEvent in your top level window, check that close event is being accepted.
  • Check if there are some other top level widgets that may be hidden but not closed. Use QApplication::topLevelWidgets() to list them.
like image 135
Pavel Strakhov Avatar answered Nov 16 '22 00:11

Pavel Strakhov