Solved QT5 + DLL loader (run-time)
-
@aha_1980 thank you for your good explanation.
Please, could you say to me what is the difference between
QPluginLoader
andQLibrary
.
Reading the QT Documentation, I saw thatQPluginLoader
is only for static plugins, but the wierd part is that they are loaded in run-time. I don't understood this very well.In my mind, the word "Static" means a library that will be placed inside the main application binary. And "static" means too that the file extension is ".lib" (Windows) or .a (Linux). Right?
My goal is to build a main application that can load QT plugins at run-time and, of course, they are not inside the main application binary.
So, what the difference about these two approachs:
QPluginLoader
andQLibrary
? -
@fem_dev said in QT5 + DLL loader (run-time):
Reading the QT Documentation, I saw that QPluginLoader is only for static plugins, but the wierd part is that they are loaded in run-time. I don't understood this very well.
AFAIK you can load plugins at runtime. At least they are not linked into your program, but rather external libraries. Qt Plugins are very well suited for Qt code that extends your main program.
In my mind, the word "Static" means a library that will be placed inside the main application binary. And "static" means too that the file extension is ".lib" (Windows) or .a (Linux). Right?
No, the extension is
.dll
or.so
. For example, Qt Creator is fully made of plugins; you can look in thebin
orlib
folder and you will see all the plugins.I use
QLibrary
myself to load external libraries (not written by me). Your Fortran code might be perfect for that.I hope I could clarify that a bit, I suggest that you have a deeper look at the Plug&Paint example and play a bit with it.
-
@aha_1980 thanks for your quick response again and your good clarification about this topic.
Well, I saw the Plug&Paint (the main application part) and Plug&Paint Basic Tools (the Plugins part) QT examples...
The main application is usingQPluginLoader
to load the "Basic Tools" static library plugins. Ok!In this QT example, the Plug&Paint application NOT compiles before compile the "Basic Tools" QT Project. I think that's because the "Basic Tools" output is a static library, and this static library (*.lib) is dependence library of Plug&Paint project.
So, first of all, I need to compile "Basic Tools" project -> so, I get the output static lib file -> and then compile "Plug&Paint" main application.
My goal is different: I would like to develop a QT main application that doesn't have any "plugin dependencies" at compile time.
The plugins must extend the main application only in run-time.My future plugins should be a mix of "Fortran + QT GUI + C++".
Question: 1
Is there a "good practice" to develop this compile-time independent "main application"?
May be examples, tutoriais, etc...Question 2:
What is the best "QT Creator project type" to develop this GUI plugin? Why?
a) QT Plugin?
b) Dynamic Lib?
c) Static Lib?
d) Other?Question 3:
What is the "best way" to load these plugins in run-time? Why?
a) UsingQPluginLoader
?
b) UsingQLibrary
? -
@fem_dev I suggest you to do a bit of information research yourself. As said, playing with existing examples also helps to understand how things work.
Me and @SGaist already gave examples how you could start your work.
Once again: plugins can be compiled separate from your app and can be loaded at runtime. That's what
QPluginLoader
helps you to do.QLibrary
is for loading external libraries, e.g. your pure FORTRAN libs.I really cannot tell you more, because I've used
QLibrary
a lot, but did not much with plugins so far.Regards
-
Hi, to add to @aha_1980: I've used both QLibrary and QPluginLoader, you could say QPluginLoader is a superset of QLibrary. I haven't looked at the source code, but QPluginLoader probably uses QLibrary to load the its plugin .dlls.
They're doing the same basic job, but QPluginLoader does much more checking and tidying up, so wiring up/talking to your .dlls in your app is easier going through QPluginLoader.
IOW, QPluginLoader is more high-level, like an automatic car, while QLibrary is more like driving a stick-shift car :-)
-
Thank you so much @aha_1980 , @SGaist and @hskoglund ....now I'm getting the main idea about
QPluginLoader
andQLibrary
. You guys help me a lot!I will try to do a simple code here.
All the best!
-
@aha_1980 this is the last doubt in this post:
When I try to follow your sugestion replacing this line:
MyPrototype sub = (MyPrototype) myLib.resolve("sub");
To:
MyPrototype sub = static_cast<MyPrototype>(myLib.resolve("sub"));
I got this compilation error:
error: static_cast from 'QFunctionPointer' (aka 'void (*)()') to 'MyPrototype' (aka 'void (*)(int *, int *, int *)') is not allowed
Coul you help me?
Thank you,
-
I've no compiler at hand now, but I thought that should work...
-
What is MyPrototype exactly ?
-
@aha_1980 said in QT5 + DLL loader (run-time):
Hi @fem_dev said in QT5 + DLL loader (run-time):
Question 1:
In the line:
MyPrototype sub = (MyPrototype) myLib.resolve("sub");I got this warning:
Use of old-style castHow to do a "new-style cast" and remove this warning?
MyPrototype sub = static_cast<MyPrototype>(myLib.resolve("sub"));
Question 2:
What is MyPrototype in this code? Why it works?You defined it yourself:
typedef void (*MyPrototype)(int*, int*, int*);
. This is just the signature of the function you are calling: avoid
function taking three pointers toint
as parameter. Note that you are defining a pointer to this function, because that is whatmyLib.resolve()
returns.Question 3:
Can I put QT GUI elements (Window, buttons, labels, etc...) inside a DLL file?Yes, but creating a real plugin with
QPluginLoader
is better suited for that.Regards
-
Please try this:
MyPrototype sub = reinterpret_cast<MyPrototype>(myLib.resolve("sub"));
I'm pretty sure that will work.
-
-