Qt 5.10 Book Examples
Calculator.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // Calculator.cpp
3 // ======================================================================
4 // This file is a part of the book
5 // "Qt 5.10 Professional programming with C++"
6 // http://qt-book.com
7 // ======================================================================
8 // Copyright (c) 2017 by Max Schlee
9 // ======================================================================
10 
11 #include <QtWidgets>
12 #include "Calculator.h"
13 
14 // ----------------------------------------------------------------------
15 Calculator::Calculator(QWidget* pwgt/*= 0*/) : QWidget(pwgt)
16 {
17  m_plcd = new QLCDNumber(12);
18  m_plcd->setSegmentStyle(QLCDNumber::Flat);
19  m_plcd->setMinimumSize(150, 50);
20 
21  QChar aButtons[4][4] = {{'7', '8', '9', '/'},
22  {'4', '5', '6', '*'},
23  {'1', '2', '3', '-'},
24  {'0', '.', '=', '+'}
25  };
26 
27  //Layout setup
28  QGridLayout* ptopLayout = new QGridLayout;
29  ptopLayout->addWidget(m_plcd, 0, 0, 1, 4);
30  ptopLayout->addWidget(createButton("CE"), 1, 3);
31 
32  for (int i = 0; i < 4; ++i) {
33  for (int j = 0; j < 4; ++j) {
34  ptopLayout->addWidget(createButton(aButtons[i][j]), i + 2, j);
35  }
36  }
37  setLayout(ptopLayout);
38 }
39 
40 // ----------------------------------------------------------------------
41 QPushButton* Calculator::createButton(const QString& str)
42 {
43  QPushButton* pcmd = new QPushButton(str);
44  pcmd->setMinimumSize(40, 40);
45  connect(pcmd, SIGNAL(clicked()), SLOT(slotButtonClicked()));
46  return pcmd;
47 }
48 
49 
50 // ----------------------------------------------------------------------
52 {
53  qreal fOperand2 = m_stk.pop().toFloat();
54  QString strOperation = m_stk.pop();
55  qreal fOperand1 = m_stk.pop().toFloat();
56  qreal fResult = 0;
57 
58  if (strOperation == "+") {
59  fResult = fOperand1 + fOperand2;
60  }
61  if (strOperation == "-") {
62  fResult = fOperand1 - fOperand2;
63  }
64  if (strOperation == "/") {
65  fResult = fOperand1 / fOperand2;
66  }
67  if (strOperation == "*") {
68  fResult = fOperand1 * fOperand2;
69  }
70  m_plcd->display(fResult);
71 }
72 
73 // ----------------------------------------------------------------------
75 {
76  QString str = ((QPushButton*)sender())->text();
77 
78  if (str == "CE") {
79  m_stk.clear();
80  m_strDisplay = "";
81  m_plcd->display("0");
82  return;
83  }
84  if (str.contains(QRegExp("[0-9]"))) {
85  m_strDisplay += str;
86  m_plcd->display(m_strDisplay.toDouble());
87  }
88  else if (str == ".") {
89  m_strDisplay += str;
90  m_plcd->display(m_strDisplay);
91  }
92  else {
93  if (m_stk.count() >= 2) {
94  m_stk.push(QString().setNum(m_plcd->value()));
95  calculate();
96  m_stk.clear();
97  m_stk.push(QString().setNum(m_plcd->value()));
98  if (str != "=") {
99  m_stk.push(str);
100  }
101  }
102  else {
103  m_stk.push(QString().setNum(m_plcd->value()));
104  m_stk.push(str);
105  m_strDisplay = "";
106  }
107  }
108 }
QPushButton * createButton(const QString &str)
Definition: Calculator.cpp:41
void slotButtonClicked()
Definition: Calculator.cpp:74
void calculate()
Definition: Calculator.cpp:51
Calculator(QWidget *pwgt=0)
Definition: Calculator.cpp:15