<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- calculator.qdoc --> <title>Calculator Example | Qt Widgets 5.12.6</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td >Qt 5.12</td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td >Calculator Example</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right"><a href="qtwidgets-index.html">Qt 5.12.6 Reference Documentation</a></td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#calculator-class-definition">Calculator Class Definition</a></li> <li class="level1"><a href="#calculator-class-implementation">Calculator Class Implementation</a></li> <li class="level1"><a href="#button-class-definition">Button Class Definition</a></li> <li class="level1"><a href="#button-class-implementation">Button Class Implementation</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">Calculator Example</h1> <span class="subtitle"></span> <!-- $$$widgets/calculator-brief --> <p>The example shows how to use signals and slots to implement the functionality of a calculator widget, and how to use <a href="qgridlayout.html">QGridLayout</a> to place child widgets in a grid.</p> <!-- @@@widgets/calculator --> <!-- $$$widgets/calculator-description --> <div class="descr"> <a name="details"></a> <div class="border"><p class="centerAlign"><img src="images/calculator-example.png" alt="" /></p></div><p class="figCaption">Screenshot of the Calculator example</p> <p>The example consists of two classes:</p> <ul> <li><code>Calculator</code> is the calculator widget, with all the calculator functionality.</li> <li><code>Button</code> is the widget used for each of the calculator button. It derives from <a href="qtoolbutton.html">QToolButton</a>.</li> </ul> <p>We will start by reviewing <code>Calculator</code>, then we will take a look at <code>Button</code>.</p> <a name="calculator-class-definition"></a> <h2 id="calculator-class-definition">Calculator Class Definition</h2> <pre class="cpp"> <span class="keyword">class</span> Calculator : <span class="keyword">public</span> <span class="type"><a href="qwidget.html">QWidget</a></span> { Q_OBJECT <span class="keyword">public</span>: Calculator(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>); <span class="keyword">private</span> <span class="keyword">slots</span>: <span class="type">void</span> digitClicked(); <span class="type">void</span> unaryOperatorClicked(); <span class="type">void</span> additiveOperatorClicked(); <span class="type">void</span> multiplicativeOperatorClicked(); <span class="type">void</span> equalClicked(); <span class="type">void</span> pointClicked(); <span class="type">void</span> changeSignClicked(); <span class="type">void</span> backspaceClicked(); <span class="type">void</span> clear(); <span class="type">void</span> clearAll(); <span class="type">void</span> clearMemory(); <span class="type">void</span> readMemory(); <span class="type">void</span> setMemory(); <span class="type">void</span> addToMemory(); </pre> <p>The <code>Calculator</code> class provides a simple calculator widget. It inherits from <a href="qdialog.html">QDialog</a> and has several private slots associated with the calculator's buttons. <a href="../qtcore/qobject.html#eventFilter">QObject::eventFilter</a>() is reimplemented to handle mouse events on the calculator's display.</p> <p>Buttons are grouped in categories according to their behavior. For example, all the digit buttons (labeled <b>0</b> to <b>9</b>) append a digit to the current operand. For these, we connect multiple buttons to the same slot (e.g., <code>digitClicked()</code>). The categories are digits, unary operators (<b>Sqrt</b>, <b>x²</b>, <b>1/x</b>), additive operators (<b>+</b>, <b>-</b>), and multiplicative operators (<b>×</b>, <b>÷</b>). The other buttons have their own slots.</p> <pre class="cpp"> <span class="keyword">private</span>: Button <span class="operator">*</span>createButton(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&</span>text<span class="operator">,</span> <span class="keyword">const</span> <span class="type">char</span> <span class="operator">*</span>member); <span class="type">void</span> abortOperation(); bool calculate(<span class="type">double</span> rightOperand<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&</span>pendingOperator); </pre> <p>The private <code>createButton()</code> function is used as part of the widget construction. <code>abortOperation()</code> is called whenever a division by zero occurs or when a square root operation is applied to a negative number. <code>calculate()</code> applies a binary operator (<b>+</b>, <b>-</b>, <b>×</b>, or <b>÷</b>).</p> <pre class="cpp"> <span class="type">double</span> sumInMemory; <span class="type">double</span> sumSoFar; <span class="type">double</span> factorSoFar; <span class="type"><a href="../qtcore/qstring.html">QString</a></span> pendingAdditiveOperator; <span class="type"><a href="../qtcore/qstring.html">QString</a></span> pendingMultiplicativeOperator; bool waitingForOperand; </pre> <p>These variables, together with the contents of the calculator display (a <a href="qlineedit.html">QLineEdit</a>), encode the state of the calculator:</p> <ul> <li><code>sumInMemory</code> contains the value stored in the calculator's memory (using <b>MS</b>, <b>M+</b>, or <b>MC</b>).</li> <li><code>sumSoFar</code> stores the value accumulated so far. When the user clicks <b>=</b>, <code>sumSoFar</code> is recomputed and shown on the display. <b>Clear All</b> resets <code>sumSoFar</code> to zero.</li> <li><code>factorSoFar</code> stores a temporary value when doing multiplications and divisions.</li> <li><code>pendingAdditiveOperator</code> stores the last additive operator clicked by the user.</li> <li><code>pendingMultiplicativeOperator</code> stores the last multiplicative operator clicked by the user.</li> <li><code>waitingForOperand</code> is <code>true</code> when the calculator is expecting the user to start typing an operand.</li> </ul> <p>Additive and multiplicative operators are treated differently because they have different precedences. For example, <b>1 + 2 ÷ 3</b> is interpreted as <b>1 + (2 ÷ 3)</b> because <b>÷</b> has higher precedence than <b>+</b>.</p> <p>The table below shows the evolution of the calculator state as the user enters a mathematical expression.</p> <div class="table"><table class="generic"> <thead><tr class="qt-style"><th >User Input</th><th >Display</th><th >Sum so Far</th><th >Add. Op.</th><th >Factor so Far</th><th >Mult. Op.</th><th >Waiting for Operand?</th></tr></thead> <tr valign="top" class="odd"><td ></td><td >0</td><td >0</td><td ></td><td ></td><td ></td><td ><code>true</code></td></tr> <tr valign="top" class="even"><td ><b>1</b></td><td >1</td><td >0</td><td ></td><td ></td><td ></td><td ><code>false</code></td></tr> <tr valign="top" class="odd"><td ><b>1 +</b></td><td >1</td><td >1</td><td ><b>+</b></td><td ></td><td ></td><td ><code>true</code></td></tr> <tr valign="top" class="even"><td ><b>1 + 2</b></td><td >2</td><td >1</td><td ><b>+</b></td><td ></td><td ></td><td ><code>false</code></td></tr> <tr valign="top" class="odd"><td ><b>1 + 2 ÷</b></td><td >2</td><td >1</td><td ><b>+</b></td><td >2</td><td ><b>÷</b></td><td ><code>true</code></td></tr> <tr valign="top" class="even"><td ><b>1 + 2 ÷ 3</b></td><td >3</td><td >1</td><td ><b>+</b></td><td >2</td><td ><b>÷</b></td><td ><code>false</code></td></tr> <tr valign="top" class="odd"><td ><b>1 + 2 ÷ 3 -</b></td><td >1.66667</td><td >1.66667</td><td ><b>-</b></td><td ></td><td ></td><td ><code>true</code></td></tr> <tr valign="top" class="even"><td ><b>1 + 2 ÷ 3 - 4</b></td><td >4</td><td >1.66667</td><td ><b>-</b></td><td ></td><td ></td><td ><code>false</code></td></tr> <tr valign="top" class="odd"><td ><b>1 + 2 ÷ 3 - 4 =</b></td><td >-2.33333</td><td >0</td><td ></td><td ></td><td ></td><td ><code>true</code></td></tr> </table></div> <p>Unary operators, such as <b>Sqrt</b>, require no special handling; they can be applied immediately since the operand is already known when the operator button is clicked.</p> <pre class="cpp"> <span class="type"><a href="qlineedit.html">QLineEdit</a></span> <span class="operator">*</span>display; <span class="keyword">enum</span> { NumDigitButtons <span class="operator">=</span> <span class="number">10</span> }; Button <span class="operator">*</span>digitButtons<span class="operator">[</span>NumDigitButtons<span class="operator">]</span>; }; </pre> <p>Finally, we declare the variables associated with the display and the buttons used to display numerals.</p> <a name="calculator-class-implementation"></a> <h2 id="calculator-class-implementation">Calculator Class Implementation</h2> <pre class="cpp"> Calculator<span class="operator">::</span>Calculator(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent) : <span class="type"><a href="qwidget.html">QWidget</a></span>(parent) { sumInMemory <span class="operator">=</span> <span class="number">0.0</span>; sumSoFar <span class="operator">=</span> <span class="number">0.0</span>; factorSoFar <span class="operator">=</span> <span class="number">0.0</span>; waitingForOperand <span class="operator">=</span> <span class="keyword">true</span>; </pre> <p>In the constructor, we initialize the calculator's state. The <code>pendingAdditiveOperator</code> and <code>pendingMultiplicativeOperator</code> variables don't need to be initialized explicitly, because the <a href="../qtcore/qstring.html">QString</a> constructor initializes them to empty strings.</p> <pre class="cpp"> display <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlineedit.html">QLineEdit</a></span>(<span class="string">"0"</span>); display<span class="operator">-</span><span class="operator">></span>setReadOnly(<span class="keyword">true</span>); display<span class="operator">-</span><span class="operator">></span>setAlignment(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>AlignRight); display<span class="operator">-</span><span class="operator">></span>setMaxLength(<span class="number">15</span>); <span class="type"><a href="../qtgui/qfont.html">QFont</a></span> font <span class="operator">=</span> display<span class="operator">-</span><span class="operator">></span>font(); font<span class="operator">.</span>setPointSize(font<span class="operator">.</span>pointSize() <span class="operator">+</span> <span class="number">8</span>); display<span class="operator">-</span><span class="operator">></span>setFont(font); </pre> <p>We create the <a href="qlineedit.html">QLineEdit</a> representing the calculator's display and set up some of its properties. In particular, we set it to be read-only.</p> <p>We also enlarge <code>display</code>'s font by 8 points.</p> <pre class="cpp"> <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator"><</span> NumDigitButtons; <span class="operator">+</span><span class="operator">+</span>i) { digitButtons<span class="operator">[</span>i<span class="operator">]</span> <span class="operator">=</span> createButton(<span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">::</span>number(i)<span class="operator">,</span> SLOT(digitClicked())); } Button <span class="operator">*</span>pointButton <span class="operator">=</span> createButton(tr(<span class="string">"."</span>)<span class="operator">,</span> SLOT(pointClicked())); Button <span class="operator">*</span>changeSignButton <span class="operator">=</span> createButton(tr(<span class="string">"\302\261"</span>)<span class="operator">,</span> SLOT(changeSignClicked())); Button <span class="operator">*</span>backspaceButton <span class="operator">=</span> createButton(tr(<span class="string">"Backspace"</span>)<span class="operator">,</span> SLOT(backspaceClicked())); Button <span class="operator">*</span>clearButton <span class="operator">=</span> createButton(tr(<span class="string">"Clear"</span>)<span class="operator">,</span> SLOT(clear())); Button <span class="operator">*</span>clearAllButton <span class="operator">=</span> createButton(tr(<span class="string">"Clear All"</span>)<span class="operator">,</span> SLOT(clearAll())); Button <span class="operator">*</span>clearMemoryButton <span class="operator">=</span> createButton(tr(<span class="string">"MC"</span>)<span class="operator">,</span> SLOT(clearMemory())); Button <span class="operator">*</span>readMemoryButton <span class="operator">=</span> createButton(tr(<span class="string">"MR"</span>)<span class="operator">,</span> SLOT(readMemory())); Button <span class="operator">*</span>setMemoryButton <span class="operator">=</span> createButton(tr(<span class="string">"MS"</span>)<span class="operator">,</span> SLOT(setMemory())); Button <span class="operator">*</span>addToMemoryButton <span class="operator">=</span> createButton(tr(<span class="string">"M+"</span>)<span class="operator">,</span> SLOT(addToMemory())); Button <span class="operator">*</span>divisionButton <span class="operator">=</span> createButton(tr(<span class="string">"\303\267"</span>)<span class="operator">,</span> SLOT(multiplicativeOperatorClicked())); Button <span class="operator">*</span>timesButton <span class="operator">=</span> createButton(tr(<span class="string">"\303\227"</span>)<span class="operator">,</span> SLOT(multiplicativeOperatorClicked())); Button <span class="operator">*</span>minusButton <span class="operator">=</span> createButton(tr(<span class="string">"-"</span>)<span class="operator">,</span> SLOT(additiveOperatorClicked())); Button <span class="operator">*</span>plusButton <span class="operator">=</span> createButton(tr(<span class="string">"+"</span>)<span class="operator">,</span> SLOT(additiveOperatorClicked())); Button <span class="operator">*</span>squareRootButton <span class="operator">=</span> createButton(tr(<span class="string">"Sqrt"</span>)<span class="operator">,</span> SLOT(unaryOperatorClicked())); Button <span class="operator">*</span>powerButton <span class="operator">=</span> createButton(tr(<span class="string">"x\302\262"</span>)<span class="operator">,</span> SLOT(unaryOperatorClicked())); Button <span class="operator">*</span>reciprocalButton <span class="operator">=</span> createButton(tr(<span class="string">"1/x"</span>)<span class="operator">,</span> SLOT(unaryOperatorClicked())); Button <span class="operator">*</span>equalButton <span class="operator">=</span> createButton(tr(<span class="string">"="</span>)<span class="operator">,</span> SLOT(equalClicked())); </pre> <p>For each button, we call the private <code>createButton()</code> function with the proper text label and a slot to connect to the button.</p> <pre class="cpp"> <span class="type"><a href="qgridlayout.html">QGridLayout</a></span> <span class="operator">*</span>mainLayout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgridlayout.html">QGridLayout</a></span>; mainLayout<span class="operator">-</span><span class="operator">></span>setSizeConstraint(<span class="type"><a href="qlayout.html">QLayout</a></span><span class="operator">::</span>SetFixedSize); mainLayout<span class="operator">-</span><span class="operator">></span>addWidget(display<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">6</span>); mainLayout<span class="operator">-</span><span class="operator">></span>addWidget(backspaceButton<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">2</span>); mainLayout<span class="operator">-</span><span class="operator">></span>addWidget(clearButton<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">2</span>); mainLayout<span class="operator">-</span><span class="operator">></span>addWidget(clearAllButton<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">4</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">2</span>); mainLayout<span class="operator">-</span><span class="operator">></span>addWidget(clearMemoryButton<span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="number">0</span>); mainLayout<span class="operator">-</span><span class="operator">></span>addWidget(readMemoryButton<span class="operator">,</span> <span class="number">3</span><span class="operator">,</span> <span class="number">0</span>); mainLayout<span class="operator">-</span><span class="operator">></span>addWidget(setMemoryButton<span class="operator">,</span> <span class="number">4</span><span class="operator">,</span> <span class="number">0</span>); mainLayout<span class="operator">-</span><span class="operator">></span>addWidget(addToMemoryButton<span class="operator">,</span> <span class="number">5</span><span class="operator">,</span> <span class="number">0</span>); <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">1</span>; i <span class="operator"><</span> NumDigitButtons; <span class="operator">+</span><span class="operator">+</span>i) { <span class="type">int</span> row <span class="operator">=</span> ((<span class="number">9</span> <span class="operator">-</span> i) <span class="operator">/</span> <span class="number">3</span>) <span class="operator">+</span> <span class="number">2</span>; <span class="type">int</span> column <span class="operator">=</span> ((i <span class="operator">-</span> <span class="number">1</span>) <span class="operator">%</span> <span class="number">3</span>) <span class="operator">+</span> <span class="number">1</span>; mainLayout<span class="operator">-</span><span class="operator">></span>addWidget(digitButtons<span class="operator">[</span>i<span class="operator">]</span><span class="operator">,</span> row<span class="operator">,</span> column); } mainLayout<span class="operator">-</span><span class="operator">></span>addWidget(digitButtons<span class="operator">[</span><span class="number">0</span><span class="operator">]</span><span class="operator">,</span> <span class="number">5</span><span class="operator">,</span> <span class="number">1</span>); mainLayout<span class="operator">-</span><span class="operator">></span>addWidget(pointButton<span class="operator">,</span> <span class="number">5</span><span class="operator">,</span> <span class="number">2</span>); mainLayout<span class="operator">-</span><span class="operator">></span>addWidget(changeSignButton<span class="operator">,</span> <span class="number">5</span><span class="operator">,</span> <span class="number">3</span>); mainLayout<span class="operator">-</span><span class="operator">></span>addWidget(divisionButton<span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="number">4</span>); mainLayout<span class="operator">-</span><span class="operator">></span>addWidget(timesButton<span class="operator">,</span> <span class="number">3</span><span class="operator">,</span> <span class="number">4</span>); mainLayout<span class="operator">-</span><span class="operator">></span>addWidget(minusButton<span class="operator">,</span> <span class="number">4</span><span class="operator">,</span> <span class="number">4</span>); mainLayout<span class="operator">-</span><span class="operator">></span>addWidget(plusButton<span class="operator">,</span> <span class="number">5</span><span class="operator">,</span> <span class="number">4</span>); mainLayout<span class="operator">-</span><span class="operator">></span>addWidget(squareRootButton<span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="number">5</span>); mainLayout<span class="operator">-</span><span class="operator">></span>addWidget(powerButton<span class="operator">,</span> <span class="number">3</span><span class="operator">,</span> <span class="number">5</span>); mainLayout<span class="operator">-</span><span class="operator">></span>addWidget(reciprocalButton<span class="operator">,</span> <span class="number">4</span><span class="operator">,</span> <span class="number">5</span>); mainLayout<span class="operator">-</span><span class="operator">></span>addWidget(equalButton<span class="operator">,</span> <span class="number">5</span><span class="operator">,</span> <span class="number">5</span>); setLayout(mainLayout); setWindowTitle(tr(<span class="string">"Calculator"</span>)); } </pre> <p>The layout is handled by a single <a href="qgridlayout.html">QGridLayout</a>. The <a href="qlayout.html#sizeConstraint-prop">QLayout::setSizeConstraint</a>() call ensures that the <code>Calculator</code> widget is always shown as its optimal size (its <a href="qwidget.html#sizeHint-prop">size hint</a>), preventing the user from resizing the calculator. The size hint is determined by the size and <a href="qwidget.html#sizePolicy-prop">size policy</a> of the child widgets.</p> <p>Most child widgets occupy only one cell in the grid layout. For these, we only need to pass a row and a column to <a href="qgridlayout.html#addWidget">QGridLayout::addWidget</a>(). The <code>display</code>, <code>backspaceButton</code>, <code>clearButton</code>, and <code>clearAllButton</code> widgets occupy more than one column; for these we must also pass a row span and a column span.</p> <pre class="cpp"> <span class="type">void</span> Calculator<span class="operator">::</span>digitClicked() { Button <span class="operator">*</span>clickedButton <span class="operator">=</span> qobject_cast<span class="operator"><</span>Button <span class="operator">*</span><span class="operator">></span>(sender()); <span class="type">int</span> digitValue <span class="operator">=</span> clickedButton<span class="operator">-</span><span class="operator">></span>text()<span class="operator">.</span>toInt(); <span class="keyword">if</span> (display<span class="operator">-</span><span class="operator">></span>text() <span class="operator">=</span><span class="operator">=</span> <span class="string">"0"</span> <span class="operator">&</span><span class="operator">&</span> digitValue <span class="operator">=</span><span class="operator">=</span> <span class="number">0.0</span>) <span class="keyword">return</span>; <span class="keyword">if</span> (waitingForOperand) { display<span class="operator">-</span><span class="operator">></span>clear(); waitingForOperand <span class="operator">=</span> <span class="keyword">false</span>; } display<span class="operator">-</span><span class="operator">></span>setText(display<span class="operator">-</span><span class="operator">></span>text() <span class="operator">+</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">::</span>number(digitValue)); } </pre> <p>Pressing one of the calculator's digit buttons will emit the button's <a href="qabstractbutton.html#clicked">clicked()</a> signal, which will trigger the <code>digitClicked()</code> slot.</p> <p>First, we find out which button sent the signal using <a href="../qtcore/qobject.html#sender">QObject::sender</a>(). This function returns the sender as a <a href="../qtcore/qobject.html">QObject</a> pointer. Since we know that the sender is a <code>Button</code> object, we can safely cast the <a href="../qtcore/qobject.html">QObject</a>. We could have used a C-style cast or a C++ <code>static_cast<>()</code>, but as a defensive programming technique we use a <a href="../qtcore/qobject.html#qobject_cast">qobject_cast</a>(). The advantage is that if the object has the wrong type, a null pointer is returned. Crashes due to null pointers are much easier to diagnose than crashes due to unsafe casts. Once we have the button, we extract the operator using <a href="qabstractbutton.html#text-prop">QToolButton::text</a>().</p> <p>The slot needs to consider two situations in particular. If <code>display</code> contains "0" and the user clicks the <b>0</b> button, it would be silly to show "00". And if the calculator is in a state where it is waiting for a new operand, the new digit is the first digit of that new operand; in that case, any result of a previous calculation must be cleared first.</p> <p>At the end, we append the new digit to the value in the display.</p> <pre class="cpp"> <span class="type">void</span> Calculator<span class="operator">::</span>unaryOperatorClicked() { Button <span class="operator">*</span>clickedButton <span class="operator">=</span> qobject_cast<span class="operator"><</span>Button <span class="operator">*</span><span class="operator">></span>(sender()); <span class="type"><a href="../qtcore/qstring.html">QString</a></span> clickedOperator <span class="operator">=</span> clickedButton<span class="operator">-</span><span class="operator">></span>text(); <span class="type">double</span> operand <span class="operator">=</span> display<span class="operator">-</span><span class="operator">></span>text()<span class="operator">.</span>toDouble(); <span class="type">double</span> result <span class="operator">=</span> <span class="number">0.0</span>; <span class="keyword">if</span> (clickedOperator <span class="operator">=</span><span class="operator">=</span> tr(<span class="string">"Sqrt"</span>)) { <span class="keyword">if</span> (operand <span class="operator"><</span> <span class="number">0.0</span>) { abortOperation(); <span class="keyword">return</span>; } result <span class="operator">=</span> std<span class="operator">::</span>sqrt(operand); } <span class="keyword">else</span> <span class="keyword">if</span> (clickedOperator <span class="operator">=</span><span class="operator">=</span> tr(<span class="string">"x\302\262"</span>)) { result <span class="operator">=</span> std<span class="operator">::</span>pow(operand<span class="operator">,</span> <span class="number">2.0</span>); } <span class="keyword">else</span> <span class="keyword">if</span> (clickedOperator <span class="operator">=</span><span class="operator">=</span> tr(<span class="string">"1/x"</span>)) { <span class="keyword">if</span> (operand <span class="operator">=</span><span class="operator">=</span> <span class="number">0.0</span>) { abortOperation(); <span class="keyword">return</span>; } result <span class="operator">=</span> <span class="number">1.0</span> <span class="operator">/</span> operand; } display<span class="operator">-</span><span class="operator">></span>setText(<span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">::</span>number(result)); waitingForOperand <span class="operator">=</span> <span class="keyword">true</span>; } </pre> <p>The <code>unaryOperatorClicked()</code> slot is called whenever one of the unary operator buttons is clicked. Again a pointer to the clicked button is retrieved using <a href="../qtcore/qobject.html#sender">QObject::sender</a>(). The operator is extracted from the button's text and stored in <code>clickedOperator</code>. The operand is obtained from <code>display</code>.</p> <p>Then we perform the operation. If <b>Sqrt</b> is applied to a negative number or <b>1/x</b> to zero, we call <code>abortOperation()</code>. If everything goes well, we display the result of the operation in the line edit and we set <code>waitingForOperand</code> to <code>true</code>. This ensures that if the user types a new digit, the digit will be considered as a new operand, instead of being appended to the current value.</p> <pre class="cpp"> <span class="type">void</span> Calculator<span class="operator">::</span>additiveOperatorClicked() { Button <span class="operator">*</span>clickedButton <span class="operator">=</span> qobject_cast<span class="operator"><</span>Button <span class="operator">*</span><span class="operator">></span>(sender()); <span class="type"><a href="../qtcore/qstring.html">QString</a></span> clickedOperator <span class="operator">=</span> clickedButton<span class="operator">-</span><span class="operator">></span>text(); <span class="type">double</span> operand <span class="operator">=</span> display<span class="operator">-</span><span class="operator">></span>text()<span class="operator">.</span>toDouble(); </pre> <p>The <code>additiveOperatorClicked()</code> slot is called when the user clicks the <b>+</b> or <b>-</b> button.</p> <p>Before we can actually do something about the clicked operator, we must handle any pending operations. We start with the multiplicative operators, since these have higher precedence than additive operators:</p> <pre class="cpp"> <span class="keyword">if</span> (<span class="operator">!</span>pendingMultiplicativeOperator<span class="operator">.</span>isEmpty()) { <span class="keyword">if</span> (<span class="operator">!</span>calculate(operand<span class="operator">,</span> pendingMultiplicativeOperator)) { abortOperation(); <span class="keyword">return</span>; } display<span class="operator">-</span><span class="operator">></span>setText(<span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">::</span>number(factorSoFar)); operand <span class="operator">=</span> factorSoFar; factorSoFar <span class="operator">=</span> <span class="number">0.0</span>; pendingMultiplicativeOperator<span class="operator">.</span>clear(); } </pre> <p>If <b>×</b> or <b>÷</b> has been clicked earlier, without clicking <b>=</b> afterward, the current value in the display is the right operand of the <b>×</b> or <b>÷</b> operator and we can finally perform the operation and update the display.</p> <pre class="cpp"> <span class="keyword">if</span> (<span class="operator">!</span>pendingAdditiveOperator<span class="operator">.</span>isEmpty()) { <span class="keyword">if</span> (<span class="operator">!</span>calculate(operand<span class="operator">,</span> pendingAdditiveOperator)) { abortOperation(); <span class="keyword">return</span>; } display<span class="operator">-</span><span class="operator">></span>setText(<span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">::</span>number(sumSoFar)); } <span class="keyword">else</span> { sumSoFar <span class="operator">=</span> operand; } </pre> <p>If <b>+</b> or <b>-</b> has been clicked earlier, <code>sumSoFar</code> is the left operand and the current value in the display is the right operand of the operator. If there is no pending additive operator, <code>sumSoFar</code> is simply set to be the text in the display.</p> <pre class="cpp"> pendingAdditiveOperator <span class="operator">=</span> clickedOperator; waitingForOperand <span class="operator">=</span> <span class="keyword">true</span>; } </pre> <p>Finally, we can take care of the operator that was just clicked. Since we don't have the right-hand operand yet, we store the clicked operator in the <code>pendingAdditiveOperator</code> variable. We will apply the operation later, when we have a right operand, with <code>sumSoFar</code> as the left operand.</p> <pre class="cpp"> <span class="type">void</span> Calculator<span class="operator">::</span>multiplicativeOperatorClicked() { Button <span class="operator">*</span>clickedButton <span class="operator">=</span> qobject_cast<span class="operator"><</span>Button <span class="operator">*</span><span class="operator">></span>(sender()); <span class="type"><a href="../qtcore/qstring.html">QString</a></span> clickedOperator <span class="operator">=</span> clickedButton<span class="operator">-</span><span class="operator">></span>text(); <span class="type">double</span> operand <span class="operator">=</span> display<span class="operator">-</span><span class="operator">></span>text()<span class="operator">.</span>toDouble(); <span class="keyword">if</span> (<span class="operator">!</span>pendingMultiplicativeOperator<span class="operator">.</span>isEmpty()) { <span class="keyword">if</span> (<span class="operator">!</span>calculate(operand<span class="operator">,</span> pendingMultiplicativeOperator)) { abortOperation(); <span class="keyword">return</span>; } display<span class="operator">-</span><span class="operator">></span>setText(<span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">::</span>number(factorSoFar)); } <span class="keyword">else</span> { factorSoFar <span class="operator">=</span> operand; } pendingMultiplicativeOperator <span class="operator">=</span> clickedOperator; waitingForOperand <span class="operator">=</span> <span class="keyword">true</span>; } </pre> <p>The <code>multiplicativeOperatorClicked()</code> slot is similar to <code>additiveOperatorClicked()</code>. We don't need to worry about pending additive operators here, because multiplicative operators have precedence over additive operators.</p> <pre class="cpp"> <span class="type">void</span> Calculator<span class="operator">::</span>equalClicked() { <span class="type">double</span> operand <span class="operator">=</span> display<span class="operator">-</span><span class="operator">></span>text()<span class="operator">.</span>toDouble(); <span class="keyword">if</span> (<span class="operator">!</span>pendingMultiplicativeOperator<span class="operator">.</span>isEmpty()) { <span class="keyword">if</span> (<span class="operator">!</span>calculate(operand<span class="operator">,</span> pendingMultiplicativeOperator)) { abortOperation(); <span class="keyword">return</span>; } operand <span class="operator">=</span> factorSoFar; factorSoFar <span class="operator">=</span> <span class="number">0.0</span>; pendingMultiplicativeOperator<span class="operator">.</span>clear(); } <span class="keyword">if</span> (<span class="operator">!</span>pendingAdditiveOperator<span class="operator">.</span>isEmpty()) { <span class="keyword">if</span> (<span class="operator">!</span>calculate(operand<span class="operator">,</span> pendingAdditiveOperator)) { abortOperation(); <span class="keyword">return</span>; } pendingAdditiveOperator<span class="operator">.</span>clear(); } <span class="keyword">else</span> { sumSoFar <span class="operator">=</span> operand; } display<span class="operator">-</span><span class="operator">></span>setText(<span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">::</span>number(sumSoFar)); sumSoFar <span class="operator">=</span> <span class="number">0.0</span>; waitingForOperand <span class="operator">=</span> <span class="keyword">true</span>; } </pre> <p>Like in <code>additiveOperatorClicked()</code>, we start by handling any pending multiplicative and additive operators. Then we display <code>sumSoFar</code> and reset the variable to zero. Resetting the variable to zero is necessary to avoid counting the value twice.</p> <pre class="cpp"> <span class="type">void</span> Calculator<span class="operator">::</span>pointClicked() { <span class="keyword">if</span> (waitingForOperand) display<span class="operator">-</span><span class="operator">></span>setText(<span class="string">"0"</span>); <span class="keyword">if</span> (<span class="operator">!</span>display<span class="operator">-</span><span class="operator">></span>text()<span class="operator">.</span>contains(<span class="char">'.'</span>)) display<span class="operator">-</span><span class="operator">></span>setText(display<span class="operator">-</span><span class="operator">></span>text() <span class="operator">+</span> tr(<span class="string">"."</span>)); waitingForOperand <span class="operator">=</span> <span class="keyword">false</span>; } </pre> <p>The <code>pointClicked()</code> slot adds a decimal point to the content in <code>display</code>.</p> <pre class="cpp"> <span class="type">void</span> Calculator<span class="operator">::</span>changeSignClicked() { <span class="type"><a href="../qtcore/qstring.html">QString</a></span> text <span class="operator">=</span> display<span class="operator">-</span><span class="operator">></span>text(); <span class="type">double</span> value <span class="operator">=</span> text<span class="operator">.</span>toDouble(); <span class="keyword">if</span> (value <span class="operator">></span> <span class="number">0.0</span>) { text<span class="operator">.</span>prepend(tr(<span class="string">"-"</span>)); } <span class="keyword">else</span> <span class="keyword">if</span> (value <span class="operator"><</span> <span class="number">0.0</span>) { text<span class="operator">.</span>remove(<span class="number">0</span><span class="operator">,</span> <span class="number">1</span>); } display<span class="operator">-</span><span class="operator">></span>setText(text); } </pre> <p>The <code>changeSignClicked()</code> slot changes the sign of the value in <code>display</code>. If the current value is positive, we prepend a minus sign; if the current value is negative, we remove the first character from the value (the minus sign).</p> <pre class="cpp"> <span class="type">void</span> Calculator<span class="operator">::</span>backspaceClicked() { <span class="keyword">if</span> (waitingForOperand) <span class="keyword">return</span>; <span class="type"><a href="../qtcore/qstring.html">QString</a></span> text <span class="operator">=</span> display<span class="operator">-</span><span class="operator">></span>text(); text<span class="operator">.</span>chop(<span class="number">1</span>); <span class="keyword">if</span> (text<span class="operator">.</span>isEmpty()) { text <span class="operator">=</span> <span class="string">"0"</span>; waitingForOperand <span class="operator">=</span> <span class="keyword">true</span>; } display<span class="operator">-</span><span class="operator">></span>setText(text); } </pre> <p>The <code>backspaceClicked()</code> removes the rightmost character in the display. If we get an empty string, we show "0" and set <code>waitingForOperand</code> to <code>true</code>.</p> <pre class="cpp"> <span class="type">void</span> Calculator<span class="operator">::</span>clear() { <span class="keyword">if</span> (waitingForOperand) <span class="keyword">return</span>; display<span class="operator">-</span><span class="operator">></span>setText(<span class="string">"0"</span>); waitingForOperand <span class="operator">=</span> <span class="keyword">true</span>; } </pre> <p>The <code>clear()</code> slot resets the current operand to zero. It is equivalent to clicking <b>Backspace</b> enough times to erase the entire operand.</p> <pre class="cpp"> <span class="type">void</span> Calculator<span class="operator">::</span>clearAll() { sumSoFar <span class="operator">=</span> <span class="number">0.0</span>; factorSoFar <span class="operator">=</span> <span class="number">0.0</span>; pendingAdditiveOperator<span class="operator">.</span>clear(); pendingMultiplicativeOperator<span class="operator">.</span>clear(); display<span class="operator">-</span><span class="operator">></span>setText(<span class="string">"0"</span>); waitingForOperand <span class="operator">=</span> <span class="keyword">true</span>; } </pre> <p>The <code>clearAll()</code> slot resets the calculator to its initial state.</p> <pre class="cpp"> <span class="type">void</span> Calculator<span class="operator">::</span>clearMemory() { sumInMemory <span class="operator">=</span> <span class="number">0.0</span>; } <span class="type">void</span> Calculator<span class="operator">::</span>readMemory() { display<span class="operator">-</span><span class="operator">></span>setText(<span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">::</span>number(sumInMemory)); waitingForOperand <span class="operator">=</span> <span class="keyword">true</span>; } <span class="type">void</span> Calculator<span class="operator">::</span>setMemory() { equalClicked(); sumInMemory <span class="operator">=</span> display<span class="operator">-</span><span class="operator">></span>text()<span class="operator">.</span>toDouble(); } <span class="type">void</span> Calculator<span class="operator">::</span>addToMemory() { equalClicked(); sumInMemory <span class="operator">+</span><span class="operator">=</span> display<span class="operator">-</span><span class="operator">></span>text()<span class="operator">.</span>toDouble(); } </pre> <p>The <code>clearMemory()</code> slot erases the sum kept in memory, <code>readMemory()</code> displays the sum as an operand, <code>setMemory()</code> replace the sum in memory with the current sum, and <code>addToMemory()</code> adds the current value to the value in memory. For <code>setMemory()</code> and <code>addToMemory()</code>, we start by calling <code>equalClicked()</code> to update <code>sumSoFar</code> and the value in the display.</p> <pre class="cpp"> Button <span class="operator">*</span>Calculator<span class="operator">::</span>createButton(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&</span>text<span class="operator">,</span> <span class="keyword">const</span> <span class="type">char</span> <span class="operator">*</span>member) { Button <span class="operator">*</span>button <span class="operator">=</span> <span class="keyword">new</span> Button(text); connect(button<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> member); <span class="keyword">return</span> button; } </pre> <p>The private <code>createButton()</code> function is called from the constructor to create calculator buttons.</p> <pre class="cpp"> <span class="type">void</span> Calculator<span class="operator">::</span>abortOperation() { clearAll(); display<span class="operator">-</span><span class="operator">></span>setText(tr(<span class="string">"####"</span>)); } </pre> <p>The private <code>abortOperation()</code> function is called whenever a calculation fails. It resets the calculator state and displays "####".</p> <pre class="cpp"> bool Calculator<span class="operator">::</span>calculate(<span class="type">double</span> rightOperand<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&</span>pendingOperator) { <span class="keyword">if</span> (pendingOperator <span class="operator">=</span><span class="operator">=</span> tr(<span class="string">"+"</span>)) { sumSoFar <span class="operator">+</span><span class="operator">=</span> rightOperand; } <span class="keyword">else</span> <span class="keyword">if</span> (pendingOperator <span class="operator">=</span><span class="operator">=</span> tr(<span class="string">"-"</span>)) { sumSoFar <span class="operator">-</span><span class="operator">=</span> rightOperand; } <span class="keyword">else</span> <span class="keyword">if</span> (pendingOperator <span class="operator">=</span><span class="operator">=</span> tr(<span class="string">"\303\227"</span>)) { factorSoFar <span class="operator">*</span><span class="operator">=</span> rightOperand; } <span class="keyword">else</span> <span class="keyword">if</span> (pendingOperator <span class="operator">=</span><span class="operator">=</span> tr(<span class="string">"\303\267"</span>)) { <span class="keyword">if</span> (rightOperand <span class="operator">=</span><span class="operator">=</span> <span class="number">0.0</span>) <span class="keyword">return</span> <span class="keyword">false</span>; factorSoFar <span class="operator">/</span><span class="operator">=</span> rightOperand; } <span class="keyword">return</span> <span class="keyword">true</span>; } </pre> <p>The private <code>calculate()</code> function performs a binary operation. The right operand is given by <code>rightOperand</code>. For additive operators, the left operand is <code>sumSoFar</code>; for multiplicative operators, the left operand is <code>factorSoFar</code>. The function return <code>false</code> if a division by zero occurs.</p> <a name="button-class-definition"></a> <h2 id="button-class-definition">Button Class Definition</h2> <p>Let's now take a look at the <code>Button</code> class:</p> <pre class="cpp"> <span class="keyword">class</span> Button : <span class="keyword">public</span> <span class="type"><a href="qtoolbutton.html">QToolButton</a></span> { Q_OBJECT <span class="keyword">public</span>: <span class="keyword">explicit</span> Button(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&</span>text<span class="operator">,</span> <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>); <span class="type"><a href="../qtcore/qsize.html">QSize</a></span> sizeHint() <span class="keyword">const</span> override; }; </pre> <p>The <code>Button</code> class has a convenience constructor that takes a text label and a parent widget, and it reimplements <a href="qwidget.html#sizeHint-prop">QWidget::sizeHint</a>() to provide more space around the text than the amount <a href="qtoolbutton.html">QToolButton</a> normally provides.</p> <a name="button-class-implementation"></a> <h2 id="button-class-implementation">Button Class Implementation</h2> <pre class="cpp"> Button<span class="operator">::</span>Button(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&</span>text<span class="operator">,</span> <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent) : <span class="type"><a href="qtoolbutton.html">QToolButton</a></span>(parent) { setSizePolicy(<span class="type"><a href="qsizepolicy.html">QSizePolicy</a></span><span class="operator">::</span>Expanding<span class="operator">,</span> <span class="type"><a href="qsizepolicy.html">QSizePolicy</a></span><span class="operator">::</span>Preferred); setText(text); } </pre> <p>The buttons' appearance is determined by the layout of the calculator widget through the size and <a href="qwidget.html#sizePolicy-prop">size policy</a> of the layout's child widgets. The call to the <a href="qwidget.html#sizePolicy-prop">setSizePolicy()</a> function in the constructor ensures that the button will expand horizontally to fill all the available space; by default, <a href="qtoolbutton.html">QToolButton</a>s don't expand to fill available space. Without this call, the different buttons in a same column would have different widths.</p> <pre class="cpp"> <span class="type"><a href="../qtcore/qsize.html">QSize</a></span> Button<span class="operator">::</span>sizeHint() <span class="keyword">const</span> { <span class="type"><a href="../qtcore/qsize.html">QSize</a></span> size <span class="operator">=</span> <span class="type"><a href="qtoolbutton.html">QToolButton</a></span><span class="operator">::</span>sizeHint(); size<span class="operator">.</span>rheight() <span class="operator">+</span><span class="operator">=</span> <span class="number">20</span>; size<span class="operator">.</span>rwidth() <span class="operator">=</span> <a href="../qtcore/qtglobal.html#qMax">qMax</a>(size<span class="operator">.</span>width()<span class="operator">,</span> size<span class="operator">.</span>height()); <span class="keyword">return</span> size; } </pre> <p>In <a href="qwidget.html#sizeHint-prop">sizeHint()</a>, we try to return a size that looks good for most buttons. We reuse the size hint of the base class (<a href="qtoolbutton.html">QToolButton</a>) but modify it in the following ways:</p> <ul> <li>We add 20 to the <a href="../qtcore/qsize.html#height">height</a> component of the size hint.</li> <li>We make the <a href="../qtcore/qsize.html#width">width</a> component of the size hint at least as much as the <a href="../qtcore/qsize.html#width">height</a>.</li> </ul> <p>This ensures that with most fonts, the digit and operator buttons will be square, without truncating the text on the <b>Backspace</b>, <b>Clear</b>, and <b>Clear All</b> buttons.</p> <p>The screenshot below shows how the <code>Calculator</code> widget would look like if we <i>didn't</i> set the horizontal size policy to <a href="qsizepolicy.html#Policy-enum">QSizePolicy::Expanding</a> in the constructor and if we didn't reimplement <a href="qwidget.html#sizeHint-prop">QWidget::sizeHint</a>().</p> <div class="border"><p class="centerAlign"><img src="images/calculator-ugly.png" alt="" /></p></div><p class="figCaption">The Calculator example with default size policies and size hints</p> <p>Files:</p> <ul> <li><a href="qtwidgets-widgets-calculator-button-cpp.html">widgets/calculator/button.cpp</a></li> <li><a href="qtwidgets-widgets-calculator-button-h.html">widgets/calculator/button.h</a></li> <li><a href="qtwidgets-widgets-calculator-calculator-cpp.html">widgets/calculator/calculator.cpp</a></li> <li><a href="qtwidgets-widgets-calculator-calculator-h.html">widgets/calculator/calculator.h</a></li> <li><a href="qtwidgets-widgets-calculator-calculator-pro.html">widgets/calculator/calculator.pro</a></li> <li><a href="qtwidgets-widgets-calculator-main-cpp.html">widgets/calculator/main.cpp</a></li> </ul> </div> <!-- @@@widgets/calculator --> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2019 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.<br/> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br/> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. </p> </div> </body> </html>