1

How can I totally remove the QApplication instance, so it can be possible to recreate it again in a sheared library. If I have such a following code:

int main(int argc, char ** argv) {
QApplication *app = new QApplication(argc, argv);
MyWindow dialog;
dialog.show();
app->exec();
return 0
}

I want to be able to delete the instance after quiting the app (closing the application after app.exec()) I have tried to do the following:

app->setQuitOnLastWindowClosed(false);
app->quit();
delete app;

But non of them could be worked. I want to remove the QApplication instance like I did not create before. The QApplication in my shared library always works if I did not define the QApplication in the main.

  1. The library (lib) and the main application (app) are using different copy of Qt.
  2. I am developing the app, the lib is imported to my app.
  3. After closing the first dialog, I am using an event to trigger the thread in the library which it is waiting for this event.
Roman Pokrovskij
  • 9,449
  • 21
  • 87
  • 142
Khaled
  • 59
  • 1
  • 12
  • As far as I know you can't. `QApplication` uses `static` or global variables that live until the program exits. – nwp Mar 30 '17 at 07:59

1 Answers1

0

You don't need to do that, because QApplication can only ever be created in the main thread. If the library and the main executable share the same Qt library, presumably the main application already created the QApplication object and will run the event loop for you. So the only thing you need to do in the library is to create some objects and the event loop will process their events.

If the main executable doesn't use Qt, then you should create the application in the main thread, prime its event loop, and let the native event loop dispatch events for you.

Alas, to directly answer your question: presumably you want to do some stuff after app.exec() returns. Your code could simply be, then:

int main(int argc, char ** argv) {
  {
     QApplication app{argc, argv};
     MyWindow dialog;
     dialog.show();
     app.exec();
   }
   doSomething();
}

If I close the first dialog, the event loop will be left. Is it possible to keep the event loop?

Yes. You could clear the Qt::WA_QuitOnClose attribute of the first dialog. Or, preferably, set it on your dialog - the application won't quit as long as there are any dialogs with QT::WA_QuitOnClose still open.

Thus, for the second dialog, call:

secondDialog->setAttribute(Qt::WA_QuitOnClose);

Alternatively, if you don't have a pointer to the dialog, you can iterate all top-level widgets and clear (or set) the attribute on all of them:

for (auto widget : qApp->topLevelWidgets())
  widget->setAttribute(Qt::QA_QuitOnClose, false);
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • What I want to do is to execute a dialog, do something, then close it. Then using another dialog from the library (which has already created the QApplication object and the app.exec). If I close the first dialog, the event loop will be left. Is it possible to keep the event loop then, so I can remove creating the QApplication and the exec from the library? or if there a way to delete the QApplication object from the main so using the QApplication in the library after closing the dialog?As I have said, without using the QApplication at all in the main, the one in the lib correctly initialized. – Khaled Mar 30 '17 at 13:54
  • Kuba: you are surely right about keeping the event loop.I could not however starting the new dialog. I could start every one alone using the QApplication instance, but I could not do it with the same instance, and could not recreate new instance. I need to totally close the first dialog, and then open the other one(should not be together, only one at a time). It will be nice if I totally delete the first instance from the memory before initializing the other one, or do you have another way to do it? – Khaled Mar 31 '17 at 07:36
  • Please edit the question to answer the following: 1. Is the library (lib) and the main application (app) using the same copy of Qt? 2. Are you the developer of both the app and the lib? 3. What triggers the showing of the second dialog? 4. Show a mock-up app and lib that demonstrates what you're trying to do. We're talking 40 lines of code in total, maybe, across two `.cpp` files. Just do it. – Kuba hasn't forgotten Monica Mar 31 '17 at 12:20