转自这位大佬
问题1:用静态Qt库编译项目后没有生成dll文件,仅生成了lib文件。
答:在pro文件中加上 CONFIG += dll。
问题2:dll中如果使用了QApplication或QWidget相关的类,报错误:QWidget: must construct a QApplication before a QWidget。
答:静态编译的dll与调用模块的QCoreApplication::instance()的地址是不同的,需要在dllMain中构造一个QApplication。
问题3:构造了QApplication后,报错误:This application failed to start because it could not find or load the Qt platform plugin "windows" in ""。
答:静态编译的dll需要导入静态的plugin,方法是首先在pro文件中加入QTPLUGIN += qwindows,然后在main.cpp中加入:
#include <QtPlugin>
Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
综上,需要特别添加的内容如下
pro文件:
CONFIG += dll
QTPLUGIN += qwindows
main.cpp文件:
#include <QApplication>
#include <qt_windows.h>
#include <QtPlugin>
Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
static QApplication* app;
static int argc = 0;
BOOL APIENTRY DllMain(HMODULE hModule, DWORD reason, LPVOID lpReserved)
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
{
app = new QApplication(argc, NULL);
break;
}
case DLL_PROCESS_DETACH:
{
delete app;
break;
}
}
return(TRUE);
}