Simple Calculation machine calculator [QT official version]

Source: Internet
Author: User
Tags emit

I have been studying QT these days.

No less painful than being raped

Exercise: let's complete the code of the CITIC number and slot application in the calculator class.

After three hours, I finally completed the game.

Just like being raped, there is a pleasure at the end.

I. Functions

A simple calculator that records results, with the input and result clearing function.

II. Implementation

The interface is separated from the computing process. Each class is a calculator class and a UI: calculatorinterface. The two classes are called by calculatorinterface and then interacted with each other.

Iii. Source Code

// Implementation of the calculator class # include "Calculator. H "# include <qmessagebox. h> calculator: calculator (qobject * parent): qobject (parent), m_lastresult (0), m_currentnumber (0), m_isadding (true) {} void calculator :: numentered (INT num) {m_currentnumber = m_currentnumber * 10 + num; // emit is placed in the base emit displaychanged (qstring: Number (m_currentnumber);} void calculator :: clear () {m_currentnumber = 0; // emit is in the base emit displaych Anged (qstring: Number (m_currentnumber);} void calculator: allclear () {m_currentnumber = 0; m_lastresult = 0; // emit is in the lower emit displaychanged (qstring:: Number (m_currentnumber);} void calculator: additionmode () {calculate (); m_isadding = true;} void calculator: subtractionmode () {calculate (); m_isadding = false;} void calculator: Calculate () {m_lastresult + = m_isadding? M_currentnumber:-m_currentnumber; m_currentnumber = 0; // emit is in the base emit displaychanged (qstring: Number (m_lastresult ));}

// Implementation of calculatorinterface # include "calculatorinterface. H "# include" ui_calculatorinterface.h "# include <qsignalmapper> # include" Calculator. H "calculatorinterface: calculatorinterface (qwidget * parent): qwidget (parent), UI (new UI: calculatorinterface), m_calculator (New calculator (this )) {UI-> setupui (this); // signal ing container qsignalmapper * m = new qsignalmapper (this ); // UI-> the clicked signal of buttonzero is mapped to the 0 connect (ui -> Buttonzero, signal (clicked (), M, slot (MAP (); m-> setmapping (ui-> buttonzero, 0 ); connect (ui-> buttonone, signal (clicked (), M, slot (MAP (); m-> setmapping (ui-> buttonone, 1 ); connect (ui-> buttontwo, signal (clicked (), M, slot (MAP (); m-> setmapping (ui-> buttontwo, 2 ); connect (ui-> buttonthree, signal (clicked (), M, slot (MAP (); m-> setmapping (ui-> buttonthree, 3 ); connect (ui-> buttonfour, signal (clicked (), M, slot (MAP (); M-> setmapping (ui-> buttonfour, 4); Connect (ui-> buttonfive, signal (clicked (), m, slot (MAP (); m-> setmapping (ui-> buttonfive, 5); Connect (ui-> buttonsix, signal (clicked (), m, slot (MAP (); m-> setmapping (ui-> buttonsix, 6); Connect (ui-> buttonseven, signal (clicked (), m, slot (MAP (); m-> setmapping (ui-> buttonseven, 7); Connect (ui-> buttoneight, signal (clicked (), m, slot (MAP (); m-> setmapping (ui-> buttoneight, 8 ); Connect (ui-> buttonnine, signal (clicked (), M, slot (MAP (); m-> setmapping (ui-> buttonnine, 9 ); connect (ui-> buttonadd, signal (clicked (), M, slot (MAP (); m-> setmapping (ui-> buttonadd, 10 ); connect (ui-> buttonallclear, signal (clicked (), M, slot (MAP (); m-> setmapping (ui-> buttonallclear, 11 ); connect (ui-> buttoncalculate, signal (clicked (), M, slot (MAP (); m-> setmapping (ui-> buttoncalculate, 12 ); connect (ui-> buttoncle Ar, signal (clicked (), M, slot (MAP (); m-> setmapping (ui-> buttonclear, 13); Connect (ui-> buttonsubtract, signal (clicked (), M, slot (MAP (); m-> setmapping (ui-> buttonsubtract, 14 ); // m_calculator class dispalaychanged is bound to the UI label, once M _... changed. The UI label will update text connect (m_calculator, signal (displaychanged (qstring), UI-> entrylabel, slot (settext (qstring ))); // when the map of M receives the signal, mmaped transmits it and binds it to the keypressed slot. keypressed calls m_calculator Method connect (M, signal (mapped (INT), this, slot (keypressed (INT); // initialize m_calculator-> allclear ();} calculatorinterface :: ~ Calculatorinterface () {Delete UI;} void calculatorinterface: keypressed (INT key) {If (Key <= 9) m_calculator-> numentered (key ); else if (Key = 10) m_calculator-> additionmode (); else if (Key = 11) m_calculator-> allclear (); else if (Key = 12) m_calculator-> calculate (); else if (Key = 13) m_calculator-> clear (); else if (Key = 14) m_calculator-> subtractionmode ();} void calculatorinterface: changeevent (qevent * E) {qwidget: changeevent (E); Switch (e-> type () {Case qevent: languagechange: UI-> retranslateui (this); break; default: break ;}}

//main#include <QtGui/QApplication>#include "calculatorinterface.h"int main(int argc, char *argv[]){    QApplication a(argc, argv);    CalculatorInterface w;    w.show();    return a.exec();}


Iv. Summary

The official implementation is very delicate. calculatorinterface is used to connect the operation class and interface class and interact with each other. The logic is clear and the code is concise.

This implementation method is worth learning.


This article is original in zeroclock, but it can be reproduced, because we are brothers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.