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

时间:2017-11-23 00:50:43

标签: c++ qt qt5

我对c ++和Qt很新,但我正在尝试创建一个简单的战舰游戏。到目前为止,我已经能够设置网格并放置船只,但我不想在主游戏窗口之前放入菜单,在那里您选择“难度”,这将决定战舰的网格大小有多大。

绘制游戏板的代码如下:

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

当我实例化“new MainWindow”时,有没有办法改变这个类中的HumanPlayer参数?

非常感谢任何帮助。谢谢:))

编辑:和我的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();
}

1 个答案:

答案 0 :(得分:1)

你必须做一些修改,为了使你的代码更容易理解你必须首先向MainWidow添加一个参数,在这种情况下它将被称为网格

<强> 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

<强> difficultyWindow.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;
};

<强> difficultyWindow.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();
}

注意:消除插槽,这种情况不需要。