Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

P.S. 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
511 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...