forked from hBuzzy/SignalsAndSlots
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget.cpp
More file actions
60 lines (45 loc) · 1.76 KB
/
Copy pathwidget.cpp
File metadata and controls
60 lines (45 loc) · 1.76 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
50
51
52
53
54
55
56
57
58
59
60
#include "widget.h"
#include <QHBoxLayout>
#include <QLabel>
#include <QPointer>
#include <QProgressBar>
#include <QPushButton>
#include "enemy.h"
#include "player.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) {
ui->setupUi(this);
Player *player = new Player(100);
Enemy *enemy = new Enemy(10);
QProgressBar *healthBar = new QProgressBar;
int minValue = 0;
healthBar->setRange(minValue, player->GetMaxHealth());
healthBar->setValue(player->GetMaxHealth());
healthBar->setTextVisible(false);
QPointer<QLabel> healthBarCaption = new QLabel;
healthBarCaption->setText("Полоска здоровья");
QPointer<QPushButton> damageButton = new QPushButton;
damageButton->setText("Нанести урон");
QWidget *widget = new QWidget;
QGridLayout *widgetLayout = new QGridLayout;
widgetLayout->addWidget(healthBarCaption, 0, 0);
widgetLayout->addWidget(healthBar, 0, 1);
widgetLayout->addWidget(damageButton, 1, 0, 1, 2);
widget->setLayout(widgetLayout);
int spacerWidth = 1;
int spacerHeight = 1;
QSpacerItem *topSpacer = new QSpacerItem(
spacerWidth, spacerHeight, QSizePolicy::Minimum, QSizePolicy::Expanding);
QSpacerItem *bottomSpacer = new QSpacerItem(
spacerWidth, spacerHeight, QSizePolicy::Minimum, QSizePolicy::Expanding);
QVBoxLayout *mainLayout = new QVBoxLayout;
setLayout(mainLayout);
mainLayout->addItem(topSpacer);
mainLayout->addWidget(widget);
mainLayout->addItem(bottomSpacer);
connect(damageButton, &QPushButton::clicked, enemy,
&Enemy::OnDamageButtonClicked);
connect(enemy, &Enemy::MakeDamage, player, &Player::TakeDamage);
connect(player, &Player::HealthChanged, healthBar, &QProgressBar::setValue);
}
Widget::~Widget() { delete ui; }