How to deal with "QPixmap: Must construct a QGuiApplication before a QPixmap"
-
Hi experts,
I am using some 3rd part dlls, wll I start the module which using these dlls, it say "QPixmap: Must construct a QGuiApplication before a QPixmap" and the program crashed.
Enviorment:
OS: Widows 11.
Qt version: 5.15.2
Complier: Qt MSVC 2019 x64I did some digging and reading https://forum.qt.io/topic/130352/how-stop-the-qpixmap-must-construct-a-qguiapplication-before-a-qpixmap-in-a-dll, I will provide some information I think it might be useful.
main.cpp
#include <QApplication> #include <QDesktopServices> #include <QGuiApplication> #include <QDesktopWidget> #include <QScreen> #include <QTextCodec> #include <QDebug> #include <DesignAndDebugger.h> #include <QLibrary> // this is application I worte #include "EnhancedTools.h" int main(int argc, char* argv[]) { // for function test! bool design_and_test = false; // bool design_and_test = true; if(design_and_test) { DesignAndDebugger::runTest(); return 0; } QTextCodec* codec = QTextCodec::codecForName("UTF-8"); QTextCodec::setCodecForLocale(codec); qDebug() << "Main application, before qApp:" << QCoreApplication::instance(); QApplication app(argc, argv); qDebug() << "Main application, after qApp:" << QCoreApplication::instance(); qDebug() << "DLL, before loading icons:" << QCoreApplication::instance(); // for test load libray, with or without it, same error QLibrary mylib("CarnacPlotQt5.dll"); if(mylib.load()) { qDebug() << "CarnacPlotQt5.dll loaded"; } qDebug() << "DLL, after loading icons:" << QCoreApplication::instance(); EnhancedTools window; // get the screen number where window create. int currentScreen = app.desktop()->screenNumber(&window); // get the geometry of the screen QRect rect = QGuiApplication::screens().at(currentScreen)->geometry(); // move to the center and top window.move((rect.width() - window.width()) / 2, 0); window.show(); return app.exec(); }
EnhancedTools.pro, these are 3rd part libs I used.
INCLUDEPATH += C:/INT/Carnac/include DEPENDPATH += C:/INT/Carnac/include INCLUDEPATH += C:/INT/Carnac/src LIBS += -LC:/INT/Carnac/lib/ -lCarnac LIBS += -LC:/INT/Carnac/lib/ -lCarnacCgm LIBS += -LC:/INT/Carnac/lib/ -lCarnacChartQt5 LIBS += -LC:/INT/Carnac/lib/ -lCarnacContour LIBS += -LC:/INT/Carnac/lib/ -lCarnacContourChartQt5 LIBS += -LC:/INT/Carnac/lib/ -lCarnacContourQt5 LIBS += -LC:/INT/Carnac/lib/ -lCarnacDeviatedWellQt5 LIBS += -LC:/INT/Carnac/lib/ -lCarnacDriverQt5 LIBS += -LC:/INT/Carnac/lib/ -lCarnacGauges LIBS += -LC:/INT/Carnac/lib/ -lCarnacGaugesQt5 LIBS += -LC:/INT/Carnac/lib/ -lCarnacMapQt5 LIBS += -LC:/INT/Carnac/lib/ -lCarnacMultiWellQt5 LIBS += -LC:/INT/Carnac/lib/ -lCarnacPlot LIBS += -LC:/INT/Carnac/lib/ -lCarnacPlotQt5 LIBS += -LC:/INT/Carnac/lib/ -lCarnacPluginQt5 LIBS += -LC:/INT/Carnac/lib/ -lCarnacPs LIBS += -LC:/INT/Carnac/lib/ -lCarnacQt5 LIBS += -LC:/INT/Carnac/lib/ -lCarnacSeisLogQt5 LIBS += -LC:/INT/Carnac/lib/ -lCarnacSeismic LIBS += -LC:/INT/Carnac/lib/ -lCarnacSeismicQt5 LIBS += -LC:/INT/Carnac/lib/ -lCarnacStylesQt5 LIBS += -LC:/INT/Carnac/lib/ -lCarnacW32 LIBS += -LC:/INT/Carnac/lib/ -lCarnacWellBoreDataQt5 LIBS += -LC:/INT/Carnac/lib/ -lCarnacWellLog LIBS += -LC:/INT/Carnac/lib/ -lCarnacWellLogQt5 LIBS += -LC:/INT/Carnac/lib/ -lCarnacWellSchematic LIBS += -LC:/INT/Carnac/lib/ -lCarnacWellSchematicQt5
I use debug model and get message like.
console output:
Any help will be appreciated!
-
@laozeng1982 said in How to deal with "QPixmap: Must construct a QGuiApplication before a QPixmap":
QLibrary mylib("CarnacPlotQt5.dll");
if(mylib.load())Why are you doing this?!
Your application already links against this DLL, there is no need to dynamically load it... -
@laozeng1982 assuming you don't have any static QPixmaps or so,
move
QApplication app(argc, argv);
to the top of your main function -
@J-Hilk Hi, I move the "QApplication app(argc, argv);" to the top and remove QLibrary statement.
but It still the same error.
-
@laozeng1982 than you have probably somewhere a static QObject
-
@J-Hilk So, how Can I found where is the static Object?
If I don't start the module using the 3rd dlls, it will not raise the error, according to the debugger information, it was in the 3rd part dlls, even I found it, I can't modify it.
-
@laozeng1982 its a dll, it's dynamically loaded -> load it after your QApplication instance is up and running.
-
@J-Hilk said in How to deal with "QPixmap: Must construct a QGuiApplication before a QPixmap":
@laozeng1982 than you have probably somewhere a static QObject
That would have been my initial comment too. But when I looked at the first post in this thread I see
- A stack trace showing me code called from some action/checkbox; and
- Console output showing code running as far as the
app.exec()
.
Assuming the OP means the error message about "no application" comes at this point how can it stem from a static
QObject
? That would happen literally at start up.