gpt4 book ai didi

c++ - 使用 QStateMachine 在菜单屏幕之间转换

转载 作者:行者123 更新时间:2023-11-28 03:39:46 26 4
gpt4 key购买 nike

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

这是我的代码:

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

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);
}

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

ma​​in.cpp:

#include <QtGui>

#include "ApplicationWindow.h"

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

ApplicationWindow window;
window.show();

return app.exec();
}

那么,如何在发生转换时触发某些行为或 Action ?

干杯。

最佳答案

要在状态转换上实际执行某些操作,您必须连接到转换的 triggered() 信号,例如

QAbstractTransition* trMainLoad = mainMenuState->addTransition(mainMenu, SIGNAL(loadGameClicked()), loadGameMenuState);
connect(trMainLoad , SIGNAL(triggered()), SLOT(...));

关于c++ - 使用 QStateMachine 在菜单屏幕之间转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9651976/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com