c++ - Must construct a QApplication before a QWidget -
everywhere "before qpaintdevice" questions , error. so, here go.
i need extern qwidget able access outside (because don't know other ways it). basically, need this: create 2 qwidgets 1 window, go first window , there hide main window , show second window created main window (although main window not main(), qwidget too).
i added
extern qwidget *widget = new qwidget
everywhere , everyhow in possible ways, , still got message. suppose, means need create qapplication (in main.cpp) , declare qwidgets. how can access qwidgets qwidgets?
code here: https://github.com/ewancoder/game/tree/qwidget_before_qapp_problem
p.s. final goal able show , hide both gamewindow.cpp , world.cpp battle.cpp (just regular class)
and btw, adding q_object , #include both don't work.
anyway, if cannot use functions 1 window another, what's point? can have 1 window in another, , in one, , 1 in another... can't last previous. after years on delphi seems strange me.
don't use extern or otherwise static variables lead creation of widget before qapplication created in main. qapplication must exist before constructor of qwidget executed.
instead of sharing variable via extern, either make other windows members of main window, , make windows known each other passing around pointers, or keep them private in mainwindow , request actions subwindows e.g. via signal/slots. generic rule, don't use global variables class members.
in following firstwindow (which supposed hide main window , secondwindow) gets main window , second window passed via pointers , 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 2 members 2 other windows, firstwindow , secondwindow: class mainwindow : public qmainwindow { ... private: firstwindow *m_firstwindow; secondwindow *m_secondwindow; };
mainwindow::mainwindow(qwidget *parent) { m_firstwindow = new firstwindow; //not pass parent want hide main window while others 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 necessary no parent passed. alternatively, use qscopedpointer delete m_firstwindow; delete m_secondwindow; }
firstwindow, inline 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. button clicked m_mainwindow->hide(); m_secondwindow->show(); } private: mainwindow* m_mainwindow; secondwindow* m_secondwindow; };
Comments
Post a Comment