-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem03.cpp
More file actions
57 lines (45 loc) · 1.67 KB
/
Copy pathproblem03.cpp
File metadata and controls
57 lines (45 loc) · 1.67 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
#include <QApplication>
#include <QColor>
#include <QBoxLayout>
#include <QObject>
#include <QPalette>
#include <QPushButton>
#include <QSizePolicy>
#include <QWidget>
void setWidgetBackground(QWidget *centralWidget, const QColor &color) {
QPalette pal = centralWidget->palette();
pal.setColor(centralWidget->backgroundRole(), color);
centralWidget->setPalette(pal);
centralWidget->setAutoFillBackground(true);
}
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
auto *window = new QWidget;
window->setWindowTitle("Problem #2");
window->resize(500, 500);
auto *canvas = new QWidget;
canvas->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
setWidgetBackground(canvas, Qt::red);
auto *toolbar = new QWidget;
auto *redButton = new QPushButton("Red", toolbar);
auto *greenButton = new QPushButton("Green", toolbar);
auto *blueButton = new QPushButton("Blue", toolbar);
QObject::connect(redButton, &QPushButton::clicked, canvas, [canvas](){
setWidgetBackground(canvas, Qt::red);
});
QObject::connect(greenButton, &QPushButton::clicked, canvas, [canvas](){
setWidgetBackground(canvas, Qt::green);
});
QObject::connect(blueButton, &QPushButton::clicked, canvas, [canvas](){
setWidgetBackground(canvas, Qt::blue);
});
auto *buttonLayout = new QHBoxLayout(toolbar);
buttonLayout->addWidget(redButton);
buttonLayout->addWidget(greenButton);
buttonLayout->addWidget(blueButton);
auto *mainLayout = new QVBoxLayout(window);
mainLayout->addWidget(canvas);
mainLayout->addWidget(toolbar);
window->show();
return QApplication::exec();
}