-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
49 lines (39 loc) · 1.89 KB
/
Copy pathmainwindow.cpp
File metadata and controls
49 lines (39 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QLabel>
#include <QtWidgets/QPushButton>
#include <QTimer>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent) // passage en paramètre de la fenêtre parente (null pour la fenêtre racine)
, ui(new Ui::MainWindow) // création des widgets décrit dans fichier .ui
{
ui->setupUi(this); // initialisation des widgets
draw_area = new DrawArea();
reset_button = new QPushButton("Reset");
random_colors = new QCheckBox("Random colors");
active_collisions = new QCheckBox("Active health points");
label = new QLabel("Number of health points:");
health_number = new QSpinBox();
health_number->setValue(50);
health_number->setMaximum(1000);
QVBoxLayout *layout = new QVBoxLayout(ui->centralwidget); // constructor’s parameter is the widget that will use the layout
layout->addWidget(draw_area);
layout->addWidget(reset_button);
layout->addWidget(random_colors);
layout->addWidget(active_collisions);
layout->addWidget(label);
layout->addWidget(health_number);
QObject::connect(reset_button, &QPushButton::clicked, draw_area, &DrawArea::reset);
QObject::connect(random_colors, &QCheckBox::checkStateChanged, draw_area, &DrawArea::randomColor);
QObject::connect(active_collisions, &QCheckBox::checkStateChanged, draw_area, &DrawArea::toggleCollisions);
QObject::connect(health_number, &QSpinBox::valueChanged, [this] (int result) {draw_area->setHealth(result);});
// Animate: Timer
auto timer = new QTimer();
QObject::connect(timer, &QTimer::timeout, draw_area, &DrawArea::animate);
timer->start(nb_milliseconds);
}
MainWindow::~MainWindow()
{
delete ui; // nécessaire car par défaut Qt n’utilise pas de unique_ptr, il est donc possible de simplifier légèrement le code généré
}