-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcalculate.cpp
77 lines (66 loc) · 2.19 KB
/
calculate.cpp
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// calculate.cpp: User input in dialog box.
#include "calculate.h"
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
Calculate w;
w.show();
return a.exec();
}
Calculate::Calculate(QWidget *parent) :
QMainWindow(parent) {
QRect rec = QApplication::primaryScreen()->geometry();
int h = rec.height(), w = rec.width();
setGeometry(42 * w / 100, 42 * h / 100,
16 * w / 100, 16 * h / 100);
setWindowTitle("Calculate");
pLabelA = new QLabel("a = ");
pLabelB = new QLabel("b = ");
pLabelSum = new QLabel("a + b = ");
pLabelProduct = new QLabel("a x b = ");
a = 2;
b = 3;
pA = new QLineEdit(QString::number(a));
pB = new QLineEdit(QString::number(b));
pSum = new QLineEdit(QString::number(a + b));
pProduct = new QLineEdit(QString::number(a * b));
QHBoxLayout *pHLayoutA = new QHBoxLayout;
pHLayoutA->addWidget(pLabelA);
pHLayoutA->addWidget(pA);
QHBoxLayout *pHLayoutB = new QHBoxLayout;
pHLayoutB->addWidget(pLabelB);
pHLayoutB->addWidget(pB);
pButton = new QPushButton("Compute a + b and a x b");
QHBoxLayout *pHLayoutSum = new QHBoxLayout;
pHLayoutSum->addWidget(pLabelSum);
pHLayoutSum->addWidget(pSum);
QHBoxLayout *pHLayoutProduct = new QHBoxLayout;
pHLayoutProduct->addWidget(pLabelProduct);
pHLayoutProduct->addWidget(pProduct);
QVBoxLayout *pAll = new QVBoxLayout;
pAll->addLayout(pHLayoutA);
pAll->addLayout(pHLayoutB);
pAll->addWidget(pButton);
pAll->addLayout(pHLayoutSum);
pAll->addLayout(pHLayoutProduct);
QWidget *pW = new QWidget(this);
pW->setLayout(pAll);
setCentralWidget(pW);
connect(pButton, SIGNAL(released()),
this, SLOT(handleButton()));
}
void Calculate::handleButton() {
QString strA = pA->text(), strB = pB->text();
bool okA, okB;
qreal a1 = strA.toDouble(&okA),
b1 = strB.toDouble(&okB);
if (!okA)
pA->setText("Incorrect input");
if (!okB)
pB->setText("Incorrect input");
if (okA && okB && (a1 != a || b1 != b)) {
a = a1;
b = b1;
pSum->setText(QString::number(a + b));
pProduct->setText(QString::number(a * b));
}
}