Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QWidget: Must construct a QApplication before a QWidget error
QtWS25 Last Chance

QWidget: Must construct a QApplication before a QWidget error

Scheduled Pinned Locked Moved General and Desktop
qapplicationqwidget
3 Posts 3 Posters 3.0k 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.
  • J Offline
    J Offline
    justiliang
    wrote on last edited by justiliang
    #1

    So I am trying to separate the instantiation of QApplication from main and put it in a class member function.

    I have a class that allows me to instantiate as many Qt windows as I want on a separate thread:

    class QtWindowInit: public QWidget {
    
      Q_OBJECT
    
    public:
    
      QtWindowInit(QWidget* parent = 0) { /* Initialize stuff */ }
    
      inline void Init() {
    
        boost::thread (StartThread());
    
      }
    
    private:
    
      inline void StartThread() {
    
        char *argv[] = {"program name", "arg1", "arg2", NULL};
        int argc = sizeof(argv) / sizeof(char*) - 1;
    
        QApplication app(argc, argv);
    
        // Code to initialize window and add QWidgets in here
    
        app.exec();
    
      }
    
      // Other class member variables here
    
    }
    

    Then I have other classes to use this QtWindowInit class to create a button for example. They do not depend on Qt, all the Qt is done in the QtWindowInit class

    class CreateButton {
    
    public:
    
      CreateButton() { /* Do stuff */ }
    
      // Other functions
    
    private:
    
      QtWindowInit _QtWindowInit; // Here is the problem
    
    }
    

    So the issue here is that the CreateButton class does not create a QApplication. When we create a CreateButton object it will then create a QtWindowInit object and will fail because QApplication has not been called yet and QtWindowInit constructor takes in the default QWidget* parent = 0 constructor. I end up with this error "QWidget: Must construct a QApplication before a QWidget". Anyone know how I can work around this problem?

    JKSHJ 1 Reply Last reply
    0
    • J justiliang

      So I am trying to separate the instantiation of QApplication from main and put it in a class member function.

      I have a class that allows me to instantiate as many Qt windows as I want on a separate thread:

      class QtWindowInit: public QWidget {
      
        Q_OBJECT
      
      public:
      
        QtWindowInit(QWidget* parent = 0) { /* Initialize stuff */ }
      
        inline void Init() {
      
          boost::thread (StartThread());
      
        }
      
      private:
      
        inline void StartThread() {
      
          char *argv[] = {"program name", "arg1", "arg2", NULL};
          int argc = sizeof(argv) / sizeof(char*) - 1;
      
          QApplication app(argc, argv);
      
          // Code to initialize window and add QWidgets in here
      
          app.exec();
      
        }
      
        // Other class member variables here
      
      }
      

      Then I have other classes to use this QtWindowInit class to create a button for example. They do not depend on Qt, all the Qt is done in the QtWindowInit class

      class CreateButton {
      
      public:
      
        CreateButton() { /* Do stuff */ }
      
        // Other functions
      
      private:
      
        QtWindowInit _QtWindowInit; // Here is the problem
      
      }
      

      So the issue here is that the CreateButton class does not create a QApplication. When we create a CreateButton object it will then create a QtWindowInit object and will fail because QApplication has not been called yet and QtWindowInit constructor takes in the default QWidget* parent = 0 constructor. I end up with this error "QWidget: Must construct a QApplication before a QWidget". Anyone know how I can work around this problem?

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      Hi, and welcome to the Qt Dev Net!

      @justiliang said:

      Anyone know how I can work around this problem?

      Don't let QtWindowInit inherit QWidget (or even QObject, for that matter).

      There are two rules to follow:

      1. QApplication must be the first QObject created in your program.
      2. All GUI-related classes (QWidget, in particular) must be created in the same thread as QApplication, and all their methods must be called from that thread only.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      1
      • T3STYT Offline
        T3STYT Offline
        T3STY
        wrote on last edited by T3STY
        #3

        I would also add to JKSH's answer that the docs state clearly:

        For any GUI application using Qt, there is precisely one QApplication object, no matter whether the application has 0, 1, 2 or more windows at any given time.
        This means that your classes using a QtWindowInit object are totally wrong because each QtWindowInit instance will create a new QApplication object+instance, which will conflict with the previous one(s) (they would not know which QApplication object should manage the events) -- unless you make it static, but then other problems arise, definitely thread unsafe.
        That's why in most Qt applications it is very often to find a QApplication (or QCoreApplication) object declaration in the very first lines of main(), and then forget about it at all.

        Although I am not a threads expert, I can think of creating a main thread containing the QApplication object and the methods to create new widgets. Then, in the other threads of your application, you should just give the main thread a hint on which widget to create.

        1 Reply Last reply
        1

        • Login

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