Must construct a QApplication before a QWidget

后端 未结 2 931
无人共我
无人共我 2020-12-11 10:20

Everywhere only just \"before QPaintDevice\" questions and nowhere is my error. So, here we go.

I need an extern QWidget to be able to get access to it from outside

相关标签:
2条回答
  • 2020-12-11 10:36

    Maybe not helping the former author, but others facing the problem. I simply got this error by mistaking a debug-library with a release one. So check your linker settings, if you are sure the implementation is done right (first instancing application and then using widgets).

    0 讨论(0)
  • 2020-12-11 10:38

    Don't use extern or otherwise static variables which lead to creation of the widget before the QApplication is created in main. The QApplication must exist before the constructor of the QWidget is executed.

    Instead of sharing the variable via extern, either make the other windows members of the main window, and then make the windows known to each other by passing around pointers, or keep them private in MainWindow and request the actions from the subwindows e.g. via signal/slots. As a generic rule, don't use global variables but class members.

    In the following FirstWindow (which is supposed hide main window and secondWindow) gets the main window and the second window passed via pointers and then just calls show/hide on them directly.

    int main(int argc, char **argv) {
        QApplication app(argc, argv);
    
        MainWindow mainWindow;
        mainWindow.show();
    
        return app.exec();
    }
    

    In main window, have two members for the two other windows, say FirstWindow and SecondWindow: class MainWindow : public QMainWindow { ... private: FirstWindow *m_firstWindow; SecondWindow *m_secondWindow; };

    MainWindow::MainWindow(QWidget *parent) {
         m_firstWindow = new FirstWindow; //not pass this as parent as you want to hide the main window while the others are visible)
         m_secondWindow = new SecondWindow;
         m_firstWindow->setMainWindow(this);
         m_firstWindow->setSecond(m_secondWindow);
         m_firstWindow->show(); //Show first window immediately, leave second window hidden
    }
    
    MainWindow::~MainWindow() {
         //Manual deletion is necessary as no parent is passed. Alternatively, use QScopedPointer
         delete m_firstWindow;
         delete m_secondWindow;
    }
    

    FirstWindow, inline for brevity:

    class FirstWindow : public QWidget {
        Q_OBJECT
    public:
        explicit FirstWindow(QWidget *parent = 0) : QWidget(parent) {}
    
        void setMainWindow(MainWindow *mainWindow) { m_mainWindow = mainWindow); }
        void setSecondWindow(SecondWindow *secondWindow) { m_secondWindow = secondWindow; }
    
    private Q_SLOTS:
         void somethingHappened() { //e.g. some button was clicked
             m_mainWindow->hide();
             m_secondWindow->show();
         }
    private: 
         MainWindow* m_mainWindow;
         SecondWindow* m_secondWindow;  
    };
    
    0 讨论(0)
提交回复
热议问题