单击不同窗口上的按钮时,请更改类的参数

  • 本文关键字:参数 按钮 窗口 单击 c++ qt qt5
  • 更新时间 :
  • 英文 :


我是C 和QT的新手,但我正在尝试创建一个简单的战舰游戏。到现在为止P>

绘制游戏板的代码如下:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "boardrender.h"
#include "playerrender.h"
#include "../BattleScrabble/BattleshipBoard.h"
#include "../BattleScrabble/HumanPlayer.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
//    BattleshipBoard *a = new BattleshipBoard(10);
//    Ship *b = new Ship(AircraftCarrier);
    ui->battleshipBoardView->setMouseTracking(true);
//    BoardRender *scene=new BoardRender(a, ui->battleshipBoardView->width(), this);
//    ui->battleshipBoardView->setScene(scene);
    ui->battleshipBoardView->resize(ui->battleshipBoardView->width(),ui->battleshipBoardView->width());
    Player *b = new HumanPlayer(10,0);
    PlayerRender *a = new PlayerRender(b, ui->battleshipBoardView,this);
}
MainWindow::~MainWindow()
{
    delete ui;
}

其中 new HumanPlayer(10,0);是网格大小。

现在有趣的部分是困难窗口:

#include "difficultywindow.h"
#include "ui_difficultywindow.h"
#include "DifficultyWindow.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
DifficultyWindow::DifficultyWindow(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DifficultyWindow)
{
    ui->setupUi(this);
}
DifficultyWindow::~DifficultyWindow()
{
    delete ui;
}
void DifficultyWindow::on_pushButton_clicked()
{
    mWindow = new MainWindow(this);
    mWindow->show();
}

当我实例化"新的mainwindow"时,是否有一种方法可以更改此类的人游戏参数?

任何帮助将不胜感激。谢谢:)

编辑:和我的main.cpp

#include "mainwindow.h"
#include <QApplication>
#include "difficultywindow.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    DifficultyWindow w;
    w.show();
    return a.exec();
}

您必须进行一些修改,才能使您的代码更容易理解,必须首先向Mainwidow添加参数,在这种情况下,它将被称为GRID

mainWindow.h

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(int grid, QWidget *parent = 0);
    ~MainWindow();
private:
    Ui::MainWindow *ui;
    Player *b;
    PlayerRender *g;
};

mainwindow.cpp

MainWindow::MainWindow(int grid, QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->battleshipBoardView->setMouseTracking(true);
    ui->battleshipBoardView->resize(ui->battleshipBoardView->width(),ui->battleshipBoardView->width());
    b = new HumanPlayer(grid, 0);
    g = new PlayerRender(b, ui->battleshipBoardView,this);
}

然后,我们将创建枚举并处理信号而不是3,我们将使用QButtonGroup

luffyywindow.h

class difficultyWindow : public QDialog
{
    Q_OBJECT
public:
    enum Difficult{
       EASY,
        MEDIUM,
        HARD
    };
    explicit difficultyWindow(QWidget *parent = 0);
    ~difficultyWindow();
private slots:
    void onClicked(int id);
private:
    Ui::difficultyWindow *ui;
    MainWindow *mWindow;
    QButtonGroup *group;
};

luffyywindow.h

difficultyWindow::difficultyWindow(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::difficultyWindow)
{
    ui->setupUi(this);
    group = new QButtonGroup(this);
    group->addButton(ui->easyDiffButton, EASY);
    group->addButton(ui->mediumDifficultyButton, MEDIUM);
    group->addButton(ui->hardDifficultyButton, HARD);
    connect(group, SIGNAL(buttonClicked(int)), this, SLOT(onClicked(int)));
}
difficultyWindow::~difficultyWindow()
{
    delete ui;
}
void difficultyWindow::onClicked(int id)
{
    int grid;
    switch (id) {
    case EASY:
        grid = 10;
        break;
    case MEDIUM:
        grid = 20;
        break;
    case HARD:
        grid = 30;
        break;
    }
    mWindow = new MainWindow(grid, this);
    mWindow->show();
}

注意:消除插槽,对于这种情况,它是不需要的。

相关内容

  • 没有找到相关文章

最新更新