QT C++實現簡單計算器(仿windows計算器普通模式)
阿新 • • 發佈:2019-01-24
寫的過程很痛,網上找不到相關的詳細的分析,所以想著發一個吧。
一
二
版本看自己喜好了,我的是5.4.2。
三
類名首字母大寫。
四
//calculator.h
#ifndef CALCULATOR_H #define CALCULATOR_H #include <QMainWindow> namespace Ui { class Calculator; } class Calculator : public QMainWindow { Q_OBJECT public: explicit Calculator(QWidget *parent = 0); ~Calculator(); enum FLAG_OPERATOR{ FLAG_OPERATOR_NONE = 0, FLAG_OPERATOR_ADD, FLAG_OPERATOR_SUB, FLAG_OPERATOR_MUL, FLAG_OPERATOR_DIV }; private slots: void on_tb_num0_clicked(); void on_tb_num1_clicked(); void on_tb_num2_clicked(); void on_tb_num3_clicked(); void on_tb_num4_clicked(); void on_tb_num5_clicked(); void on_tb_num6_clicked(); void on_tb_num7_clicked(); void on_tb_num8_clicked(); void on_tb_num9_clicked(); void on_tb_add_clicked(); void on_tb_equ_clicked(); void on_tb_mul_clicked(); void on_tb_clear_clicked(); void on_tb_div_clicked(); void on_tb_sub_clicked(); private: Ui::Calculator *ui; void clickedNumber(const QString &t); void middleResult(void); void model(); bool m_firstOperator; QString m_value; QString m_leftValue; QString m_rightValue; FLAG_OPERATOR m_operator; QString m_expretion; QString m_result; int m_model; }; #endif // CALCULATOR_H
其中用巨集代替了“ + - x / ” 槽有0~9和“+ - x / ”
//calculator.cpp
#include "calculator.h" #include "ui_calculator.h" Calculator::Calculator(QWidget *parent) : QMainWindow(parent), ui(new Ui::Calculator) { ui->setupUi(this); on_tb_clear_clicked(); m_model = 0; } Calculator::~Calculator() { delete ui; } void Calculator::on_tb_num0_clicked() { clickedNumber(ui->tb_num0->text()); } void Calculator::on_tb_num1_clicked() { clickedNumber(ui->tb_num1->text()); } void Calculator::on_tb_num2_clicked() { clickedNumber(ui->tb_num2->text()); } void Calculator::on_tb_num3_clicked() { clickedNumber(ui->tb_num3->text()); } void Calculator::on_tb_num4_clicked() { clickedNumber(ui->tb_num4->text()); } void Calculator::on_tb_num5_clicked() { clickedNumber(ui->tb_num5->text()); } void Calculator::on_tb_num6_clicked() { clickedNumber(ui->tb_num6->text()); } void Calculator::on_tb_num7_clicked() { clickedNumber(ui->tb_num7->text()); } void Calculator::on_tb_num8_clicked() { clickedNumber(ui->tb_num8->text()); } void Calculator::on_tb_num9_clicked() { clickedNumber(ui->tb_num9->text()); } void Calculator::clickedNumber(const QString &t) { m_value += t; m_expretion += t; ui->lb_expretion->setText(m_expretion); } void Calculator::on_tb_add_clicked() { middleResult(); m_operator = FLAG_OPERATOR_ADD; m_expretion += ui->tb_add->text(); ui->lb_expretion->setText(m_expretion); } void Calculator::on_tb_mul_clicked() { middleResult(); m_operator = FLAG_OPERATOR_MUL; m_expretion += ui->tb_mul->text(); ui->lb_expretion->setText(m_expretion); } void Calculator::on_tb_div_clicked() { middleResult(); m_operator = FLAG_OPERATOR_DIV; m_expretion += ui->tb_div->text(); ui->lb_expretion->setText(m_expretion); } void Calculator::on_tb_sub_clicked() { middleResult(); m_operator = FLAG_OPERATOR_SUB; m_expretion += ui->tb_sub->text(); ui->lb_expretion->setText(m_expretion); } void Calculator::middleResult() { if(m_firstOperator) { m_firstOperator = false; m_leftValue = m_value; m_value.clear(); }else { m_rightValue = m_value; m_value.clear(); switch (m_operator) { case FLAG_OPERATOR_ADD: m_leftValue = QString::number(m_leftValue.toInt() + m_rightValue.toInt()); on_tb_equ_clicked(); break; case FLAG_OPERATOR_SUB: m_leftValue = QString::number(m_leftValue.toInt() - m_rightValue.toInt()); on_tb_equ_clicked(); break; case FLAG_OPERATOR_MUL: m_leftValue = QString::number(m_leftValue.toInt() * m_rightValue.toInt()); on_tb_equ_clicked(); break; case FLAG_OPERATOR_DIV: if(m_rightValue == "0") { Calculator::on_tb_clear_clicked(); ui->lb_result->setText("除數不能為0"); } else { m_leftValue = QString::number(m_leftValue.toInt() / m_rightValue.toInt()); on_tb_equ_clicked(); } break; default: break; } } } void Calculator::on_tb_equ_clicked() { m_rightValue = m_value; m_value.clear(); switch (m_operator) { case FLAG_OPERATOR_ADD: m_result = QString::number(m_leftValue.toInt() + m_rightValue.toInt()); break; case FLAG_OPERATOR_MUL: m_result = QString::number(m_leftValue.toInt() * m_rightValue.toInt()); break; case FLAG_OPERATOR_DIV: m_result = QString::number(m_leftValue.toInt() / m_rightValue.toInt()); break; case FLAG_OPERATOR_SUB: m_result = QString::number(m_leftValue.toInt() - m_rightValue.toInt()); break; default: break; } ui->lb_result->setText(m_result); } void Calculator::on_tb_clear_clicked() { m_firstOperator = true; m_operator = FLAG_OPERATOR_NONE; m_value.clear(); m_expretion.clear(); m_result.clear(); m_leftValue.clear(); m_rightValue.clear(); ui->lb_expretion->setText("0"); ui->lb_result->setText("0"); }
四則運算部分:將運算部分封裝到了on_tb_equ_clicked()即“=”的槽中(偷個懶~~~),on_tb_clear_clicked()函式實現運算子左,右值的清除和兩個顯示
視窗的內容清除。
錯誤報告部分:除數不能為0
case FLAG_OPERATOR_DIV: if(m_rightValue == "0") { Calculator::on_tb_clear_clicked(); ui->lb_result->setText("除數不能為0"); } else { m_leftValue = QString::number(m_leftValue.toInt() / m_rightValue.toInt()); on_tb_equ_clicked(); } break;
還有一些兒我沒寫(偷懶)比如:連續輸入運算子錯誤;結果的數值過大無法正常顯示;
//main.cpp
#include "calculator.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Calculator w;
w.show();
return a.exec();
}
都能看懂不說了。
//calculator.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Calculator</class>
<widget class="QMainWindow" name="Calculator">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>336</width>
<height>406</height>
</rect>
</property>
<property name="windowTitle">
<string>Calculator</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QWidget" name="layoutWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>0</y>
<width>301</width>
<height>331</height>
</rect>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="4" column="4">
<widget class="QToolButton" name="toolButton_9">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="4" column="3">
<widget class="QToolButton" name="toolButton_7">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="4" column="2">
<widget class="QToolButton" name="tb_clear">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>C</string>
</property>
</widget>
</item>
<item row="5" column="4">
<widget class="QToolButton" name="toolButton_14">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="5" column="3">
<widget class="QToolButton" name="tb_div">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>÷</string>
</property>
</widget>
</item>
<item row="5" column="2">
<widget class="QToolButton" name="tb_num9">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>9</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QToolButton" name="tb_num8">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>8</string>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QToolButton" name="tb_num7">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>7</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QToolButton" name="toolButton_8">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QToolButton" name="toolButton_6">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="3" column="4">
<widget class="QToolButton" name="toolButton_5">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="3" column="3">
<widget class="QToolButton" name="toolButton_4">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="3" column="2">
<widget class="QToolButton" name="toolButton_3">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QToolButton" name="toolButton_2">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QToolButton" name="toolButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QToolButton" name="tb_num4">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>4</string>
</property>
</widget>
</item>
<item row="6" column="1">
<widget class="QToolButton" name="tb_num5">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>5</string>
</property>
</widget>
</item>
<item row="6" column="2">
<widget class="QToolButton" name="tb_num6">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>6</string>
</property>
</widget>
</item>
<item row="6" column="3">
<widget class="QToolButton" name="tb_mul">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>×</string>
</property>
</widget>
</item>
<item row="6" column="4">
<widget class="QToolButton" name="toolButton_26">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="7" column="0">
<widget class="QToolButton" name="tb_num1">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>1</string>
</property>
</widget>
</item>
<item row="7" column="1">
<widget class="QToolButton" name="tb_num2">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>2</string>
</property>
</widget>
</item>
<item row="7" column="2">
<widget class="QToolButton" name="tb_num3">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>3</string>
</property>
</widget>
</item>
<item row="7" column="3">
<widget class="QToolButton" name="tb_sub">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>-</string>
</property>
</widget>
</item>
<item row="8" column="2">
<widget class="QToolButton" name="toolButton_19">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>.</string>
</property>
</widget>
</item>
<item row="8" column="3">
<widget class="QToolButton" name="tb_add">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>+</string>
</property>
</widget>
</item>
<item row="8" column="0" colspan="2">
<widget class="QToolButton" name="tb_num0">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>0</string>
</property>
</widget>
</item>
<item row="7" column="4" rowspan="2">
<widget class="QToolButton" name="tb_equ">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>=</string>
</property>
</widget>
</item>
<item row="1" column="0" colspan="5">
<widget class="QLabel" name="lb_result">
<property name="font">
<font>
<family>黑體</family>
<pointsize>14</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>0</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="0" column="0" colspan="5">
<widget class="QLabel" name="lb_expretion">
<property name="font">
<font>
<family>黑體</family>
<pointsize>14</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>0</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="2" column="2">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>10</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>336</width>
<height>23</height>
</rect>
</property>
<widget class="QMenu" name="menu">
<property name="title">
<string> 選單</string>
</property>
<addaction name="action"/>
<addaction name="action_2"/>
<addaction name="action_3"/>
<addaction name="separator"/>
<addaction name="action_5"/>
</widget>
<widget class="QMenu" name="menu_2">
<property name="title">
<string>幫助</string>
</property>
<addaction name="actionHelp"/>
</widget>
<addaction name="menu"/>
<addaction name="menu_2"/>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
<action name="action">
<property name="text">
<string>普通</string>
</property>
</action>
<action name="action_2">
<property name="text">
<string>科學</string>
</property>
</action>
<action name="action_3">
<property name="text">
<string>程式猿</string>
</property>
</action>
<action name="action_5">
<property name="text">
<string>退出</string>
</property>
</action>
<action name="actionHelp">
<property name="text">
<string>help</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
新人都是畫的,所以還是貼個圖吧。
為了完整的模仿win7的計算器但又很多功能目前實現不了,只能用“...”代替了。
先寫這麼多等會了再帖,如有用,請君收藏。