Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Must Construct QApplication before QWidget Error
daye last edited by
I'm running this Application which is a c# wrapper for OBS Studio and it runs fine when I build in Visual Studios, however when I try to run the .exe I get this error message "QWidget: Must construct a QApplication before a Qwidget"
If I remove the Qt5Cored.dll from the directory the application runs but then crashes if I mouse over the main display panel So that solution is not useful.
I get this error message "QWidget: Must construct a QApplication before a Qwidget"
Well, you must, you need to fix your code.
mostefa last edited by mostefa
I am not familiar with your code , but i know that the:
"QWidget: Must construct a QApplication before a Qwidget"
Happen when you have on your main.cpp an instance of widget before an instance of QApplication:
this code will produce your error:
MainWindow w; QApplication a(argc, argv); w.show(); return a.exec();
if you change it to the following code
QApplication a(argc, argv); MainWindow w; w.show(); return a.exec();
Everything will be ok
I hope that my answer will help you.
Always make sure your application is the first thing constructed in your main and it should work fine. :) @mostefa showed a perfect example of why/how this may happen.
If you are using a library that is making widgets before your application starts (which it sounds like you may be doing) then you will need to delay load that lib so your application is created before it attempts to create any widgets.
mostefa last edited by
And to be more precise
This is mentioned in QApplication doc:
This application object must be constructed before any paint devices (including widgets, pixmaps, bitmaps etc.).
And to be more precise
Or to be even more precise, it should be the very first
QObjectto be created and the very last to be destroyed, as is discussed here.
I'll just post the documentation quotation here for posterity:
In general, creating QObjects before the QApplication is not supported and can lead to weird crashes on exit, depending on the platform. This means static instances of QObject are also not supported. A properly structured single or multi-threaded application should make the QApplication be the first created, and last destroyed QObject.