Must Construct QApplication before QWidget Error
-
wrote on 19 Feb 2017, 10:07 last edited by
https://github.com/GoaLitiuM/libobs-sharp
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.
-
https://github.com/GoaLitiuM/libobs-sharp
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.
@daye said in Must Construct QApplication before QWidget Error:
I get this error message "QWidget: Must construct a QApplication before a Qwidget"
Well, you must, you need to fix your code.
-
wrote on 19 Feb 2017, 15:51 last edited by mostefa
Hi @daye
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.
-
wrote on 19 Feb 2017, 18:33 last edited by
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.
-
wrote on 19 Feb 2017, 18:45 last edited by
And to be more precise
This is mentioned in QApplication doc:
http://doc.qt.io/Qt-5/qapplication.html#details
This application object must be constructed before any paint devices (including widgets, pixmaps, bitmaps etc.).
-
And to be more precise
This is mentioned in QApplication doc:
http://doc.qt.io/Qt-5/qapplication.html#details
This application object must be constructed before any paint devices (including widgets, pixmaps, bitmaps etc.).
@mostefa said in Must Construct QApplication before QWidget Error:
And to be more precise
Or to be even more precise, it should be the very first
QObject
to 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.
1/6