Must Construct QApplication before QWidget Error



  • 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"
    0_1487498658969_upload-9fc8b1b3-a39f-446c-981f-9e19714b1139

    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.


  • Qt Champions 2016

    @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.



  • 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.


  • Moderators

    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.



  • 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.).
    

  • Qt Champions 2016

    @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.