I'd like to know what are the implications (problems) of having multiple QApplication
/QCoreApplication
instances in the same process, and how to solve some issues regarding it.
The scenario is as follows: I'd like to make a wrapper on a open source third-party application in order to convert it into an embeddable widget as an optional plugin (the application consists basically on a single QMainWindow
-based interface).
Such project heavily relies on a QCoreApplication
derived class but basically because it is used as an already existing singleton. I'm able to modify the code (and I'll have to do so in order to expose the QMainWindow
as an embeddable widget), although for the complexity of that project I cannot simply remove the parent class.
So, the final application will have its own QApplication
(created on start) and then will optionally load the aforementioned plugin (thus creating the second QCoreApplication
). Only the first (main) QApplication
is used for events loop (QCoreApplication::exec()
).
I'm aware of the facts that QCoreApplication
is a singleton. In my tests, the singleton always points to the last created instance:
qDebug() << qApp;
auto app1 = new QApplication(argc, argv);
qDebug() << qApp;
auto app2 = new TheOtherQApplication(argc, argv);
qDebug() << qApp;
The output is
QObject(0x0)
QApplication(0x6f9400, name = "test")
ASSERT failure in QCoreApplication: "there should be only one application object", file kernel\qcoreapplication.cpp, line 595
TheOtherQApplication(0x2550dc0, name = "test")
TheOtherQApplication(0x2550dc0, name = "test") TheOtherQApplication(0x2550dc0, name = "test")
As it can be seen, after the second QApplication
is created it replaces the global instance. Is there any way to solve this? As the plugin is optional the obvious answer (loading the main QApplication
on second place) is not a suitable option.
Also, are there any other implications of having multiple QApplication
instances? Or are all related to the events loop (checked) and the singleton?
Note: This is a project based on Qt 4.7 due to third-party dependencies not fully updated yet. It is planned to be migrated to the latest version in a year or so, but for the moment I have to deal with 4.7.
BTW, I've already reviewed several related questions, including this one but that doesn't provide any useful information.