GameView.h:
Код: Выделить всё
#pragma once
#include
#include "GameGrid.h"
namespace {
constexpr size_t CELL_SIZE = 15;
}
class GameView : public QGraphicsView {
Q_OBJECT
public:
explicit GameView(QWidget *parent = nullptr);
public slots:
void updateField();
protected:
void mousePressEvent(QMouseEvent *event) override;
private:
GameGrid field_;
size_t cellSize_ {CELL_SIZE};
};
Код: Выделить всё
#include "GameView.h"
#include
#include
GameView::GameView(QWidget *parent) : QGraphicsView(parent) {
setScene(new QGraphicsScene(0, 0, 680, 480, this));
for (size_t x = 0; x < GameGrid::FIELD_COLS; x++) {
for (size_t y = 0; y < GameGrid::FIELD_ROWS; y++) {
scene()->addRect(cellSize_ * x, cellSize_ * y, cellSize_, cellSize_, QPen(Qt::gray), QBrush(Qt::white));
}
}
}
void GameView::updateField() {
field_.updateGrid();
for (auto *item : scene()->items()) {
auto *rectItem = dynamic_cast(item);
size_t x = rectItem->x() / CELL_SIZE;
size_t y = rectItem->y() / CELL_SIZE;
if (!field_.isCellChanged(x, y))
continue;
QColor newColor = field_.getCellState(x, y) ? Qt::black : Qt::white;
rectItem->setBrush(newColor);
}
}
void GameView::mousePressEvent(QMouseEvent *event) {
if (event->buttons() & Qt::LeftButton) {
QPointF scenePos = event->scenePosition();
size_t x = scenePos.x() / cellSize_;
size_t y = scenePos.y() / cellSize_;
field_.setCellState(x, y, true);
updateField();
}
}
Код: Выделить всё
#pragma once
#include
#include
#include "GameView.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
void showField();
~MainWindow();
public slots:
void startSimulation();
void stopSimulation();
private:
GameView *visualManager;
Ui::MainWindow *ui;
QTimer *timer;
};
Код: Выделить всё
#include "mainwindow.h"
#include "./ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow),
timer(new QTimer(parent)), visualManager(new GameView(this)) {
ui->setupUi(this);
visualManager->setGeometry(10, 10, 640, 480);
connect(ui->StartButton, &QPushButton::clicked, this, &MainWindow::startSimulation);
connect(ui->StopButton, &QPushButton::clicked, this, &MainWindow::stopSimulation);
connect(timer, &QTimer::timeout, visualManager, &::GameView::updateField);
}
MainWindow::~MainWindow() {
delete visualManager;
delete timer;
delete ui;
}
void MainWindow::startSimulation() {
timer->start(100);
}
void MainWindow::stopSimulation() {
timer->stop();
}
Код: Выделить всё
#pragma once
#include
#include
class GameGrid {
public:
size_t countAliveNeighbours(size_t x, size_t y) const;
void setCellState(size_t x, size_t y, bool state);
bool getCellState(size_t x, size_t y) const;
bool isCellChanged(size_t x, size_t y) const;
void updateGrid();
void clearGrid();
static constexpr size_t FIELD_ROWS = 100;
static constexpr size_t FIELD_COLS = 100;
private:
std::bitset grid_;
std::bitset nextGrid_;
};
Я проверил это с помощью отладчика и qDebug(), но метод просто не вызывается.
Подробнее здесь: https://stackoverflow.com/questions/793 ... not-called
Мобильная версия