Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. 3rd Party Software
  4. Must Construct QApplication before QWidget Error

Must Construct QApplication before QWidget Error

Scheduled Pinned Locked Moved Unsolved 3rd Party Software
6 Posts 4 Posters 5.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    daye
    wrote on 19 Feb 2017, 10:07 last edited by
    #1

    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.

    K 1 Reply Last reply 19 Feb 2017, 13:23
    0
    • D daye
      19 Feb 2017, 10:07

      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.

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 19 Feb 2017, 13:23 last edited by
      #2

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

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      2
      • M Offline
        M Offline
        mostefa
        wrote on 19 Feb 2017, 15:51 last edited by mostefa
        #3

        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.

        1 Reply Last reply
        2
        • A Offline
          A Offline
          ambershark
          wrote on 19 Feb 2017, 18:33 last edited by
          #4

          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.

          My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

          1 Reply Last reply
          2
          • M Offline
            M Offline
            mostefa
            wrote on 19 Feb 2017, 18:45 last edited by
            #5

            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.).
            
            K 1 Reply Last reply 19 Feb 2017, 19:37
            0
            • M mostefa
              19 Feb 2017, 18:45

              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.).
              
              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 19 Feb 2017, 19:37 last edited by
              #6

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

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              2

              1/6

              19 Feb 2017, 10:07

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved