c++ - Transitioning between menu screens with QStateMachine -
i considering transitioning between menu screens in game using qstatemachine
. however, i'm unsure how kick off code (e.g. show()
qwidget
) upon transition between states occurring. can quite plain old signals (see commented out code), figure fancy animation upon switching screens using transitions.
here's code:
edit: updated per koying's suggestion.
applicationwindow.h:
#include <qtgui> #include <qstatemachine> #include "mainmenu.h" #include "loadgamemenu.h" class applicationwindow : public qmainwindow { q_object public: applicationwindow(); private slots: void mainmenubuttonclicked(); void loadgamemenubuttonclicked(); private: mainmenu* mainmenu; loadgamemenu* loadgamemenu; qstatemachine statemachine; qstackedwidget* stack; };
applicationwindow.cpp:
applicationwindow::applicationwindow() { resize(800, 600); stack = new qstackedwidget(this); mainmenu = new mainmenu(); setcentralwidget(mainmenu); loadgamemenu = new loadgamemenu(); qstate* mainmenustate = new qstate(); qstate* loadgamemenustate = new qstate(); qabstracttransition* loadtransition = mainmenustate->addtransition( mainmenu, signal(loadgameclicked()), loadgamemenustate); connect(loadtransition, signal(triggered()), this, slot(loadgamemenubuttonclicked())); qabstracttransition* mainmenutransition = loadgamemenustate->addtransition( loadgamemenu, signal(backtomainmenuclicked()), mainmenustate); connect(mainmenutransition, signal(triggered()), this, slot(mainmenubuttonclicked())); statemachine.addstate(mainmenustate); statemachine.addstate(loadgamemenustate); statemachine.setinitialstate(mainmenustate); statemachine.start(); } void applicationwindow::mainmenubuttonclicked() { setcentralwidget(mainmenu); } void applicationwindow::loadgamemenubuttonclicked() { setcentralwidget(loadgamemenu); }
loadgamemenu.h:
#include <qtgui> class loadgamemenu : public qwidget { q_object public: loadgamemenu(); signals: void backtomainmenuclicked(); private: qpushbutton* loadgamebutton; qpushbutton* backtomainmenubutton; };
loadgamemenu.cpp:
#include "loadgamemenu.h" loadgamemenu::loadgamemenu() { loadgamebutton = new qpushbutton(tr("load")); backtomainmenubutton = new qpushbutton(tr("main menu")); qobject::connect(backtomainmenubutton, signal(clicked()), this, signal(backtomainmenuclicked())); qvboxlayout* layout = new qvboxlayout(); layout->addwidget(loadgamebutton); layout->addwidget(backtomainmenubutton); layout->setcontentsmargins(300, 400, 300, 200); setlayout(layout); }
mainmenu.h:
#include <qtgui> class mainmenu : public qwidget { q_object public: mainmenu(); signals: void newgameclicked(); void loadgameclicked(); private slots: void exit(); private: qpushbutton* newgamebutton; qpushbutton* loadgamebutton; qpushbutton* exitgamebutton; qmenu* filemenu; };
mainmenu.cpp:
#include "mainmenu.h" mainmenu::mainmenu() { newgamebutton = new qpushbutton(tr("new game"), this); loadgamebutton = new qpushbutton(tr("load game")); exitgamebutton = new qpushbutton(tr("exit")); qobject::connect(newgamebutton, signal(clicked()), this, signal(newgameclicked())); qobject::connect(loadgamebutton, signal(clicked()), this, signal(loadgameclicked())); qobject::connect(exitgamebutton, signal(clicked()), qapp, slot(quit())); qvboxlayout* layout = new qvboxlayout(); layout->addwidget(newgamebutton); layout->addwidget(loadgamebutton); layout->addwidget(exitgamebutton); layout->setcontentsmargins(300, 200, 300, 200); setlayout(layout); } void mainmenu::exit() { if( qmessagebox::question( this, tr("exit?"), tr("do want exit game?"), qmessagebox::yes | qmessagebox::no, qmessagebox::no ) == qmessagebox::yes ) { qapp->quit(); } }
main.cpp:
#include <qtgui> #include "applicationwindow.h" int main(int argv, char **args) { qapplication app(argv, args); applicationwindow window; window.show(); return app.exec(); }
so, how trigger behaviour or action when transition occurs?
cheers.
to on state transition, have connect triggered()
signal of transition, e.g.
qabstracttransition* trmainload = mainmenustate->addtransition(mainmenu, signal(loadgameclicked()), loadgamemenustate); connect(trmainload , signal(triggered()), slot(...));
Comments
Post a Comment