Qt - QStackedLayout setCurrentIndex displays blank window instead of widget -
i started learning qt couple days ago make game, , i'm trying figure out how make layouts work.
what want window using qstackedlayout 2 widgets inside: startscreen , gamescreen. startscreen shown on top on program run. button in startscren connected function inside window, , window call setcurrentindex change widget on top gamescreen.
what happens right when click on button, view changes blank window. i've tried hardcoding setcurrentindex(0), nothing, setcurrentindex(1), gamescreen should , displays same blank window, , setcurrentindex(2), out of index bounds still nothing. connection going through, don't understand why blank window show instead of button have on gamescreen.
if can explain me concept missed , how fix it, i'd appreciate it. thanks!
here window.cpp:
window::window(qwidget *parent) : qwidget(parent) { resize(640, 480); layout = new qstackedlayout; createstartscreen(); creategamescreen(); setlayout(layout); show(); }; void window::createstartscreen(){ start = new startscreen(); layout->addwidget(start); start->setwindow(this); } void window::playgame(){ layout->setcurrentindex(layout->indexof(game)); } void window::creategamescreen(){ game = new gamescreen(); layout->addwidget(game); } startscreen.cpp:
startscreen::startscreen(qwidget *parent) : qwidget(parent) { newgamebutton = new qpushbutton("new game", this); newgamebutton->setgeometry(qrect(qpoint(260, 300), qsize(120,40))); quitbutton = new qpushbutton("quit", this); quitbutton->setgeometry(qrect(qpoint(260, 360), qsize(120,40))); connect(quitbutton, signal(clicked()), qapplication::instance(), slot(quit())); }; void startscreen::setwindow(window *w){ connect(newgamebutton, signal(clicked()), w, slot(playgame())); } gamescreen.cpp:
gamescreen::gamescreen(qwidget *parent) : qwidget(parent) { button = new qpushbutton("hi"); button->setgeometry(qrect(qpoint(260, 260), qsize(120,40))); };
that's because don't call show on button. should rather use layout handle it.
e.g.:
gamescreen::gamescreen(qwidget *parent) : qwidget(parent) { button = new qpushbutton(tr("hi")); qhboxlayout *layout = new qhboxlayout(this); layout->addwidget(button); }; button have size adapted text , cleaned positioned in gamescreen.
on side note, should rather add signal startscreen request new game , connection in window. way won't have tight coupling between startscreen , window.
Comments
Post a Comment