Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7615323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:37:06+00:00 2026-05-31T02:37:06+00:00

I am considering transitioning between menu screens in a game by using QStateMachine .

  • 0

I am considering transitioning between menu screens in a game by using QStateMachine. However, I’m unsure how to kick off some code (e.g. show() a QWidget) upon a transition between states occurring. I can do it quite easily with plain old signals (see commented out code), but I figure that I could probably do some fancy animation upon switching screens using transitions.

Here’s my code:

Edit: updated as 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 you really want to exit the 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 do I trigger some behaviour or action when a transition occurs?

Cheers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:37:08+00:00Added an answer on May 31, 2026 at 2:37 am

    To actually do something on a state transition, you have to connect to the triggered() signal of the transition, e.g.

    QAbstractTransition* trMainLoad = mainMenuState->addTransition(mainMenu, SIGNAL(loadGameClicked()), loadGameMenuState);
    connect(trMainLoad , SIGNAL(triggered()), SLOT(...));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Considering the code below: How is the str1 object created without using new String()
Considering that I use code like (with or without using (considering that using(??) may
Considering the code: <noscript><div>FOO</div></noscript> Running $('noscript').html(); returns &lt;div&gt;FOO&lt;/div&gt; but running $('noscript').text(); returns the raw
Considering following code public class A { public static void main(String[] args) { new
Considering this article Developing a MVC component for Joomla , following is the code
Considering this code: std::vector<myObject*> veryLargeArray; for (int i = 0; i < veryLargeArray.size(); ++i)
Considering the following c code: typedef struct ELE *tree_ptr struct ELE { long val;
Considering this basic code snippet: <? class mcClassington { var $mcProperty='default'; function mcDoSomething() {
Considering the below code : int main() { int pid; pid=vfork(); if(pid==0) printf(child\n); else
Considering such code: class ToBeTested { public: void doForEach() { for (vector<Contained>::iterator it =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.