Qt app stays in memory even after MainWindow is cl

2019-06-18 21:47发布

问题:

The problem is as follows: if the application is closed while it's not actively doing anything, it exits correctly. If it's actively working on something (waiting in a while loop, for example), however, the main window will close but the program will continue to run in the background, as confirmed by opening the task manager.

I've spent a good part of today googling the problem and implementing possible fixes, but to no avail. It seems like the quit() function simply doesn't do anything. Here are some things that I've tried:

  • Using app.connect( &app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));. I know the signal is triggered, because I tried changing &app, SLOT(quit()) to qApp, SLOT(aboutQt()) and the aboutQt window briefly popped up.
  • Including qApp->quit(); at the beginning of a function that runs from a main window button. The application does run to that line, but it has no effect.
  • Using processEvents() to make sure the GUI is being updated.
  • Including statements like mainWindow.setAttribute(Qt::WA_QuitOnClose); in main.cpp.

I just want the application to completely exit when the main window is closed.

I'm using the qextserialport library, if that makes any difference.

回答1:

I don't know what's wrong, but as your slot actually receives the signal, you can call exit() from that slot function as a workaround.