简体   繁体   中英

Must construct a QApplication before a QWidget

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 (because I don't know any other ways to do it). Basically, I need this: Create 2 QWidgets from 1 window, go to first window and from there hide main window and show second window created by main window (although main window is not main(), it is QWidget too).

I added

extern QWidget *widget = new QWidget

everywhere and everyhow in possible ways, and I still got this message. I suppose, it means that I need to create my QApplication (in main.cpp) and only then declare any QWidgets. But then HOW can I access those QWidgets from another QWidgets?

Code is here: https://github.com/ewancoder/game/tree/QWidget_before_QApp_problem

PS The final goal is to be able show and hide both gamewindow.cpp and world.cpp from battle.cpp (just regular class)

And btw, adding Q_OBJECT and #include both don't work.

Anyway, if I cannot use functions from one window to another, than what's the point? I can have one window in another, and then another in that one, and then one in that another... but I can't do anything from the last to the previous. After years on Delphi that seems strange to me.

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 eg 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;  
};

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM