@pedisChen said in When calling the Shared Libraries (DLL) created by QT6, the main program will crash:
My dll file was built with QT6, and then I created another project with QT6 and tried to call it
You don't "call" DLLs, you link them.
Then I added the generated file to my project directory, but it's still the same.
This is not what @J-Hilk was talking about.
If you export/deploy your qt widget library, you need to ship it with all the other libs your library needs.
@pedisChen said in When calling the Shared Libraries (DLL) created by QT6, the main program will crash:
I think it is because the dll lacks of an QT event loop mechanism. so I added an init function in dll,and need to call the init function first , defined like this:
QCoreApplication a(argc, argv);
return a.exec();
A library doesn't necessarily need that.
A Qt library can be a QWidget, just a colletion of functions or something else...
None of these need their own QCoreApplication, when used in other Qt apps.
(They do in general in order to work, but at least not their own. The main loop from the app to which your library is linked to, is fine).
See this example:
If you look at the code, there is no main.cpp or additional QApplication involved.
But one is needed to use the library widget in your main app.
https://doc.qt.io/qt-6/qtdesigner-customwidgetplugin-example.html
Code: https://code.qt.io/cgit/qt/qttools.git/tree/examples/designer/customwidgetplugin?h=6.8
Edit:
@pedisChen said in When calling the Shared Libraries (DLL) created by QT6, the main program will crash:
This worked, but it would always block the main program. Then I planned to use QThread to create a new process in the dll to run a.exec(), but it failed and the new process task could not run.I dont know why...
In what way do you use your library? Or how do you intend to use it in some application?
How does your library work? Do you export a single widget, which uses QTCPSocket in your library?
Another observation:
If you don't have an event loop in your main application (when no Qt involved in main app), you obviously can't create a QThread which then should host the QApplication instance... QThread is a wrapper for std::thread and is a QObject itself. Therefore it also needs a QApplication to work properly.
Use a plain std::thread instead.