0

我正在考虑使用QStateMachine. 但是,我不确定如何在状态之间发生转换时启动一些代码(例如show()a )。QWidget我可以用普通的旧信号很容易地做到这一点(见注释掉的代码),但我认为我可以在使用过渡切换屏幕时做一些花哨的动画。

这是我的代码:

编辑:根据 Koying 的建议更新。

应用程序窗口.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;
};

应用程序窗口.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);
}

加载游戏菜单.h:

#include <QtGui>

class LoadGameMenu : public QWidget
{
    Q_OBJECT
public:
    LoadGameMenu();
signals:
    void backToMainMenuClicked();
private:
    QPushButton* loadGameButton;
    QPushButton* backToMainMenuButton;
};

加载游戏菜单.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);
}

主菜单.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;
};

主菜单.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();
    }
}

主.cpp:

#include <QtGui>

#include "ApplicationWindow.h"

int main(int argv, char **args)
{
    QApplication app(argv, args);

    ApplicationWindow window;
    window.show();

    return app.exec();
}

那么,当转换发生时如何触发一些行为或动作呢?

干杯。

4

1 回答 1

2

要在状态转换上实际做某事,您必须连接到triggered()转换的信号,例如

QAbstractTransition* trMainLoad = mainMenuState->addTransition(mainMenu, SIGNAL(loadGameClicked()), loadGameMenuState);
connect(trMainLoad , SIGNAL(triggered()), SLOT(...));
于 2012-03-11T11:50:24.820 回答