Can I use Qt in plugins?
-
There are some programs have ability to load and unload modules dynamically at runtime, but not using Qt framework(such us vlc). The "plugins" imply these modules.
If I want to provide a image-related plugin, can I use QtGUI (QImage) to implement it? As the documentation said, there should be a QGuiApplication instance. The questions are, where should it be instantiated and who maitain it?For any GUI application using Qt, there is precisely one QGuiApplication object no matter whether the application has 0, 1, 2 or more windows at any given time. For non-GUI Qt applications, use QCoreApplication instead, as it does not depend on the Qt GUI module. For QWidget based Qt applications, use QApplication instead, as it provides some functionality needed for creating QWidget instances.
Consider the following cases:
case 1:
If there are two plugins called PA and PB, they both rely on QGuiApplication, PA is loaded before PB. At PA's initialization, PA attemp to get an instance of application with QCoreApplication::instance(), and get a NULL, then PA instantiate a QGuiApplication. There is an instance and PB will be loaded. PB can't create an another QGuiApplication instance, but it can obtain the exist one. Now, PA finish its work and is about to be unloaded. Should PA destroy the instance of QGuiApplication while PB is still using it?
case 2:
PA is a plugin rely on QtGui, PB is a plugin rely on QtWighet. PA is loaded before PB, PA create a QGuiApplication at its initialization, how can PB create a QApplication instance?BTW, actually there has been a plugin based on QtWidget for vlc, it is a gui module, but it dosn't consider that there may have been an instance of QCoreApplication or its subclass, it just instantiate QApplication directly. Is this dangerous?
-
@yr.zh said in Can I use Qt in plugins?:
If I want to provide a image-related plugin, can I use QtGUI (QImage) to implement it?
Yes.
Which classes do you plan to use? I know that QPixmap definitely needs a QGuiApplication instance, but there's a chance that QImage might not. (I'm not 100% sure, so do check and report back)
As the documentation said, there should be a QGuiApplication instance. The questions are, where should it be instantiated and who maitain it?
These are design decisions that you need to make.
Consider the following cases:
case 1:
If there are two plugins called PA and PB, they both rely on QGuiApplication, PA is loaded before PB. At PA's initialization, PA attemp to get an instance of application with QCoreApplication::instance(), and get a NULL, then PA instantiate a QGuiApplication. There is an instance and PB will be loaded. PB can't create an another QGuiApplication instance, but it can obtain the exist one. Now, PA finish its work and is about to be unloaded. Should PA destroy the instance of QGuiApplication while PB is still using it?If PA destroys the QGuiApplication, then PB will stop working.
case 2:
PA is a plugin rely on QtGui, PB is a plugin rely on QtWighet. PA is loaded before PB, PA create a QGuiApplication at its initialization, how can PB create a QApplication instance?If PA has already created a QGuiApplication, then PB cannot create a QApplication in the same process.
NOTE: If PA creates a QApplication, then PB can use that instance. However, PB must make sure its code (especially widget functions) runs in PA's thread!
BTW, actually there has been a plugin based on QtWidget for vlc, it is a gui module, but it dosn't consider that there may have been an instance of QCoreApplication or its subclass, it just instantiate QApplication directly. Is this dangerous?
I wouldn't call it dangerous, but it means this plugin is incompatible with other Qt-based plugins.