QT实现一个简单的计算器

QT实现一个简单的计算器

https://blog.csdn.net/a1069962325/article/details/47067121

最近几天在学习QT,在了解了信号和槽机制、布局管理、还有一些控件的使用后,便试着写了一个计算器,带自定义的快捷键,剪贴板(复制和粘贴)的操作,有 菜单栏->选择 里的功能还未实现,等啥时候有时间把它补上吧。还有许多不足和需要修改的地方,希望各位大牛能帮忙指出其中的问题,谢谢。

下面就贴出我的代码:

calcMainWindow.h:

 

[cpp] view plain copy
 
  1. #ifndef CALCMAINWINDOW_H  
  2. #define CALCMAINWINDOW_H  
  3.   
  4. #include <QMainWindow>  
  5. #include <QLineEdit>  
  6. #include <QPushButton>  
  7. #include <QGridLayout>  
  8. #include <QString>  
  9. #include <QVector>  
  10. #include <QKeyEvent>  
  11. #include <QLabel>  
  12. #include <QMenu>  
  13. #include <QAction>  
  14.   
  15. class MainWindow : public QMainWindow  
  16. {  
  17.     Q_OBJECT  
  18.   
  19. public:  
  20.     MainWindow(QWidget *parent = 0);  
  21.     ~MainWindow();  
  22.   
  23.     QString temp;  
  24.   
  25.     double number;  
  26.     void operation();           //运算  
  27.     void keyPressEvent(QKeyEvent *);       //键盘响应  
  28.     void creatAction();         //创建菜单栏选项  
  29.     void creatMenu();           //创建菜单  
  30.   
  31. private:  
  32.     //菜单控件(功能尚未添加)  
  33.     QMenu *chooseMenu;          //“选择”菜单  
  34.     QMenu *editMenu;            //“编辑”菜单  
  35.     QMenu *helpMenu;            //“帮助”菜单  
  36.   
  37.     QAction *standerAction;     //标准  
  38.     QAction *scienceAction;     //科学  
  39.     QAction *historyAction;     //历史记录  
  40.     QAction *dateAction;        //日期计算  
  41.     QAction *copyAction;        //复制  
  42.     QAction *pasteAction;       //粘贴  
  43.     QAction *helpAction;        //查看帮助  
  44.     QAction *aboutAction;       //关于  
  45.   
  46.     //计算器相关控件  
  47.     QLabel *operationLabel;  
  48.     QLabel *resultLabel;  
  49.   
  50.     QLineEdit *showTextEdit;    //表达式显示栏  
  51.     QLineEdit *resultTextEdit;  //结果显示栏  
  52.     QPushButton *backBtn;       //回退  
  53.     QPushButton *ceBtn;         //清除  
  54.     QPushButton *clearBtn;      //清空  
  55.     QPushButton *oneBtn;  
  56.     QPushButton *twoBtn;  
  57.     QPushButton *threeBtn;  
  58.     QPushButton *fourBtn;  
  59.     QPushButton *fiveBtn;  
  60.     QPushButton *sixBtn;  
  61.     QPushButton *sevenBtn;  
  62.     QPushButton *eightBtn;  
  63.     QPushButton *nineBtn;  
  64.     QPushButton *zeroBtn;  
  65.     QPushButton *pointBtn;      //.  
  66.     QPushButton *signBtn;       //+/-  
  67.   
  68.     //四则运算  
  69.     QPushButton *addBtn;  
  70.     QPushButton *subBtn;  
  71.     QPushButton *mulBtn;  
  72.     QPushButton *divideBtn;  
  73.     QPushButton *isBtn;  
  74.   
  75. public slots:  
  76.     void button_1_click();  
  77.     void button_2_click();  
  78.     void button_3_click();  
  79.     void button_4_click();  
  80.     void button_5_click();  
  81.     void button_6_click();  
  82.     void button_7_click();  
  83.     void button_8_click();  
  84.     void button_9_click();  
  85.     void button_0_click();  
  86.   
  87.     void button_add_click();  
  88.     void button_sub_click();  
  89.     void button_mul_click();  
  90.     void button_divide_click();  
  91.     void button_is_click();  
  92.   
  93.     void button_cls_click();  
  94.     void button_ce_click();  
  95.     void button_back_click();  
  96.     void button_point_click();  
  97.     void button_sign_click();  
  98.   
  99.     void helpMessageBox();  
  100.     void aboutMessageBox();  
  101.   
  102.     void copyResult();  
  103.     void pasteNumber();  
  104. };  
  105.   
  106. #endif // CALCMAINWINDOW_H  


calcMainWindow.cpp:

 

[cpp] view plain copy
 
  1. #include "calcMainWindow.h"  
  2. #include <QHBoxLayout>  
  3. #include <QVBoxLayout>  
  4. #include <QRect>  
  5. #include <QMenuBar>  
  6. #include <QMessageBox>  
  7. #include <QClipboard>  
  8. #include <QApplication>  
  9.   
  10. bool num2 = false;      //判断是否只有一个数  
  11. int operator1 = 0;      //运算符标记:+(1),-(2),*(3),/(4)  
  12. double result = 0;      //结果  
  13. bool flag = false;      //标记分母是否为0  
  14.   
  15. MainWindow::MainWindow(QWidget *parent)  
  16.     : QMainWindow(parent)  
  17. {  
  18.     QWidget *centerWindow = new QWidget;  
  19.     this->setCentralWidget(centerWindow);  
  20.   
  21.     creatAction();  
  22.     creatMenu();  
  23.   
  24.     //标签  
  25.     operationLabel = new QLabel("Operation:");  
  26.     resultLabel = new QLabel("Result:");  
  27.     //显示栏  
  28.     showTextEdit = new QLineEdit;  
  29.     showTextEdit->setReadOnly(true);    //设为只读属性  
  30.     resultTextEdit = new QLineEdit;  
  31.     showTextEdit->setReadOnly(true);  
  32.   
  33.     //键盘按钮  
  34.     backBtn = new QPushButton("←");  
  35.     ceBtn = new QPushButton("CE");  
  36.     clearBtn = new QPushButton("CLS");  
  37.     oneBtn = new QPushButton("1");  
  38.     twoBtn = new QPushButton("2");  
  39.     threeBtn = new QPushButton("3");  
  40.     fourBtn = new QPushButton("4");  
  41.     fiveBtn = new QPushButton("5");  
  42.     sixBtn = new QPushButton("6");  
  43.     sevenBtn = new QPushButton("7");  
  44.     eightBtn = new QPushButton("8");  
  45.     nineBtn = new QPushButton("9");  
  46.     zeroBtn = new QPushButton("0");  
  47.     pointBtn = new QPushButton(".");  
  48.     isBtn = new QPushButton("=");  
  49.     isBtn->setStyleSheet("background-color: rgb(255,80,20)");  
  50.     addBtn = new QPushButton("+");  
  51.     subBtn = new QPushButton("-");  
  52.     mulBtn = new QPushButton("*");  
  53.     divideBtn = new QPushButton("/");  
  54.     signBtn = new QPushButton("+/-");  
  55.     //设置布局  
  56.     QHBoxLayout *H0 = new QHBoxLayout;  
  57.     H0->addWidget(operationLabel);  
  58.     H0->addWidget(resultLabel);  
  59.     H0->setStretch(0,2);  
  60.     H0->setStretch(1,1);  
  61.   
  62.     QHBoxLayout *H1 = new QHBoxLayout;  
  63.     H1->addWidget(showTextEdit);  
  64.     H1->addWidget(resultTextEdit);  
  65.     H1->setStretch(0,2);  
  66.     H1->setStretch(1,1);  
  67.   
  68.     QHBoxLayout *H2 = new QHBoxLayout;  
  69.     H2->addWidget(backBtn);  
  70.     H2->addWidget(ceBtn);  
  71.     H2->addWidget(clearBtn);  
  72.     H2->addWidget(addBtn);  
  73.   
  74.     QHBoxLayout *H3 = new QHBoxLayout;  
  75.     H3->addWidget(sevenBtn);  
  76.     H3->addWidget(eightBtn);  
  77.     H3->addWidget(nineBtn);  
  78.     H3->addWidget(subBtn);  
  79.   
  80.     QHBoxLayout *H4 = new QHBoxLayout;  
  81.     H4->addWidget(fourBtn);  
  82.     H4->addWidget(fiveBtn);  
  83.     H4->addWidget(sixBtn);  
  84.     H4->addWidget(mulBtn);  
  85.   
  86.     QHBoxLayout *H5 = new QHBoxLayout;  
  87.     H5->addWidget(oneBtn);  
  88.     H5->addWidget(twoBtn);  
  89.     H5->addWidget(threeBtn);  
  90.     H5->addWidget(divideBtn);  
  91.   
  92.     QHBoxLayout *H6 = new QHBoxLayout;  
  93.     H6->addWidget(signBtn);  
  94.     H6->addWidget(zeroBtn);  
  95.     H6->addWidget(pointBtn);  
  96.     H6->addWidget(isBtn);  
  97.   
  98.     QVBoxLayout *mainLayout = new QVBoxLayout;  
  99.     mainLayout->addLayout(H0);  
  100.     mainLayout->addLayout(H1);  
  101.     mainLayout->addLayout(H2);  
  102.     mainLayout->addLayout(H3);  
  103.     mainLayout->addLayout(H4);  
  104.     mainLayout->addLayout(H5);  
  105.     mainLayout->addLayout(H6);  
  106.   
  107.     connect(oneBtn, SIGNAL(clicked()), this, SLOT(button_1_click()));  
  108.     connect(twoBtn, SIGNAL(clicked()), this, SLOT(button_2_click()));  
  109.     connect(threeBtn, SIGNAL(clicked()), this, SLOT(button_3_click()));  
  110.     connect(fourBtn, SIGNAL(clicked()), this, SLOT(button_4_click()));  
  111.     connect(fiveBtn, SIGNAL(clicked()), this, SLOT(button_5_click()));  
  112.     connect(sixBtn, SIGNAL(clicked()), this, SLOT(button_6_click()));  
  113.     connect(sevenBtn, SIGNAL(clicked()), this, SLOT(button_7_click()));  
  114.     connect(eightBtn, SIGNAL(clicked()), this, SLOT(button_8_click()));  
  115.     connect(nineBtn, SIGNAL(clicked()), this, SLOT(button_9_click()));  
  116.     connect(zeroBtn, SIGNAL(clicked()), this, SLOT(button_0_click()));  
  117.   
  118.     connect(addBtn, SIGNAL(clicked()), this, SLOT(button_add_click()));  
  119.     connect(subBtn, SIGNAL(clicked()), this, SLOT(button_sub_click()));  
  120.     connect(mulBtn, SIGNAL(clicked()), this, SLOT(button_mul_click()));  
  121.     connect(divideBtn, SIGNAL(clicked()), this, SLOT(button_divide_click()));  
  122.     connect(isBtn, SIGNAL(clicked()), this, SLOT(button_is_click()));  
  123.     connect(signBtn, SIGNAL(clicked()), this, SLOT(button_sign_click()));  
  124.   
  125.     connect(clearBtn, SIGNAL(clicked()), this, SLOT(button_cls_click()));  
  126.     connect(ceBtn, SIGNAL(clicked()), this, SLOT(button_ce_click()));  
  127.     connect(backBtn, SIGNAL(clicked()), this, SLOT(button_back_click()));  
  128.     connect(pointBtn, SIGNAL(clicked()), this, SLOT(button_point_click()));  
  129.   
  130.     centerWindow->setLayout(mainLayout);  
  131. }  
  132.   
  133. MainWindow::~MainWindow()  
  134. {  
  135.   
  136. }  
  137.   
  138. /* 
  139.  **创建菜单栏选项 
  140.  */  
  141. void MainWindow::creatAction()  
  142. {  
  143.     standerAction = new QAction("&标准",this);  
  144.     standerAction->setShortcut(tr("Alt+1"));       //设置快捷键  
  145.     standerAction->setStatusTip("标准计算器");       //设置状态栏  
  146.   
  147.     scienceAction = new QAction("&科学",this);  
  148.     scienceAction->setShortcut(tr("Alt+2"));  
  149.     scienceAction->setStatusTip("科学计算器");  
  150.   
  151.     historyAction = new QAction("&历史记录",this);  
  152.     historyAction->setShortcut(tr("Alt+3"));  
  153.     historyAction->setStatusTip("历史记录");  
  154.   
  155.     dateAction = new QAction("&日期计算",this);  
  156.     dateAction->setShortcut(tr("Alt+4"));  
  157.     dateAction->setStatusTip("日期计算");  
  158.   
  159.     copyAction = new QAction("&复制",this);  
  160.     copyAction->setShortcut(tr("Ctrl+C"));  
  161.     copyAction->setStatusTip("复制结果");  
  162.     connect(copyAction, SIGNAL(triggered()), this, SLOT(copyResult()));  
  163.   
  164.     pasteAction = new QAction("&粘贴",this);  
  165.     pasteAction->setShortcut(tr("Ctrl+V"));  
  166.     pasteAction->setStatusTip("粘贴");  
  167.     connect(pasteAction, SIGNAL(triggered()), this, SLOT(pasteNumber()));  
  168.   
  169.     helpAction = new QAction("&查看帮助",this);  
  170.     helpAction->setShortcut(tr("F1"));  
  171.     helpAction->setStatusTip("查看帮助");  
  172.     connect(helpAction, SIGNAL(triggered()), this, SLOT(helpMessageBox()));  
  173.   
  174.     aboutAction = new QAction("&关于",this);  
  175.     aboutAction->setShortcut(tr("F2"));  
  176.     aboutAction->setStatusTip("关于");  
  177.     connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutMessageBox()));  
  178.   
  179. }  
  180.   
  181. /* 
  182.  **创建菜单栏 
  183.  */  
  184. void MainWindow::creatMenu()  
  185. {  
  186.     //选择菜单  
  187.     chooseMenu=menuBar()->addMenu("&选择(C)");  
  188.     chooseMenu->addAction(standerAction);       //添加按钮  
  189.     chooseMenu->addAction(scienceAction);  
  190.     chooseMenu->addSeparator();                 //添加横线  
  191.     chooseMenu->addAction(historyAction);  
  192.     chooseMenu->addAction(dateAction);  
  193.   
  194.     //编辑菜单  
  195.     editMenu=menuBar()->addMenu("&编辑(E)");  
  196.     editMenu->addAction(copyAction);  
  197.     editMenu->addAction(pasteAction);  
  198.   
  199.     //帮助菜单  
  200.     helpMenu=menuBar()->addMenu("&帮助(H)");  
  201.     helpMenu->addAction(helpAction);  
  202.     helpMenu->addSeparator();  
  203.     helpMenu->addAction(aboutAction);  
  204. }  
  205.   
  206. /* 
  207. ** 使用消息盒子创建关于窗口 
  208. */  
  209. void MainWindow::aboutMessageBox()  
  210. {  
  211.     QMessageBox::about(NULL, "关于", "计算器 V1.0 \r\n 2015.07.26");  
  212. }  
  213.   
  214. void MainWindow::helpMessageBox()  
  215. {  
  216.     QMessageBox::about(NULL, "帮助", "这儿没有任何帮助!");  
  217. }  
  218.   
  219. void MainWindow::copyResult()  
  220. {  
  221.     QClipboard *copy = QGuiApplication::clipboard();    //剪贴板操作  
  222.     temp = QString::number(result);  
  223.     copy->setText(temp);                    //复制到剪切板  
  224. }  
  225.   
  226. void MainWindow::pasteNumber()  
  227. {  
  228.     QClipboard *paste = QGuiApplication::clipboard();  
  229.     temp = paste->text();  
  230.     showTextEdit->setText(temp);            //粘贴到剪切板  
  231. }  
  232.   
  233. /* 
  234. **按下按钮后存入temp并在label上显示 
  235. */  
  236. void MainWindow::button_1_click()  
  237. {  
  238.     temp += "1";  
  239.     showTextEdit->setText(temp);  
  240. }  
  241.   
  242. void MainWindow::button_2_click()  
  243. {  
  244.     temp += "2";  
  245.     showTextEdit->setText(temp);  
  246. }  
  247.   
  248. void MainWindow::button_3_click()  
  249. {  
  250.     temp += "3";  
  251.     showTextEdit->setText(temp);  
  252. }  
  253.   
  254. void MainWindow::button_4_click()  
  255. {  
  256.     temp += "4";  
  257.     showTextEdit->setText(temp);  
  258. }  
  259.   
  260. void MainWindow::button_5_click()  
  261. {  
  262.     temp += "5";  
  263.     showTextEdit->setText(temp);  
  264. }  
  265.   
  266. void MainWindow::button_6_click()  
  267. {  
  268.     temp += "6";  
  269.     showTextEdit->setText(temp);  
  270. }  
  271.   
  272. void MainWindow::button_7_click()  
  273. {  
  274.     temp += "7";  
  275.     showTextEdit->setText(temp);  
  276. }  
  277.   
  278. void MainWindow::button_8_click()  
  279. {  
  280.     temp += "8";  
  281.     showTextEdit->setText(temp);  
  282. }  
  283.   
  284. void MainWindow::button_9_click()  
  285. {  
  286.     temp += "9";  
  287.     showTextEdit->setText(temp);  
  288. }  
  289.   
  290. void MainWindow::button_0_click()  
  291. {  
  292.     bool ok;  
  293.     double tem = temp.toDouble(&ok);  
  294.   
  295.     //无用的0不能输入  
  296.     if(1 == temp.contains(".") || 0 != tem || NULL == temp)  
  297.     {  
  298.         temp += "0";  
  299.     }  
  300.     showTextEdit->setText(temp);  
  301. }  
  302.   
  303. void MainWindow::button_add_click()  
  304. {  
  305.     if(false == num2)  
  306.     {  
  307.         num2 = true;  
  308.     }  
  309.   
  310.     operation();  
  311.     operator1 = 1;          //operator1用来存储运算符  
  312.     showTextEdit->setText("+");  
  313. }  
  314.   
  315. void MainWindow::button_sub_click()  
  316. {  
  317.   
  318.     if(false == num2)  
  319.     {  
  320.         num2 = true;  
  321.     }  
  322.   
  323.     operation();  
  324.     operator1 = 2;  
  325.     showTextEdit->setText("-");  
  326. }  
  327.   
  328. void MainWindow::button_mul_click()  
  329. {  
  330.     if(false == num2)  
  331.     {  
  332.         num2 = true;  
  333.     }  
  334.   
  335.     operation();  
  336.     operator1 = 3;  
  337.     showTextEdit->setText("*");  
  338. }  
  339.   
  340. void MainWindow::button_divide_click()  
  341. {  
  342.     if(false == num2)  
  343.     {  
  344.         num2 = true;  
  345.     }  
  346.   
  347.     operation();  
  348.     operator1 = 4;  
  349.     showTextEdit->setText("/");  
  350. }  
  351.   
  352. void MainWindow::operation()  
  353. {  
  354.     bool ok;  
  355.     if(NULL == temp)  
  356.     {  
  357.         number = 0;  
  358.     }  
  359.     else  
  360.     {  
  361.         number = temp.toDouble(&ok);  
  362.     }  
  363.   
  364.     //通过operator1选择相应的运算  
  365.     switch(operator1)  
  366.     {  
  367.     case 0:result = number;break;  
  368.     case 1:result += number;break;  
  369.     case 2:result -= number;break;  
  370.     case 3:result *= number;break;  
  371.     case 4:  
  372.         if(number == 0)  
  373.         {  
  374.             flag = true;  
  375.             break;  
  376.         }  
  377.         result /= number;  
  378.         break;  
  379.     default:  
  380.         break;  
  381.     }  
  382.     temp = "";  
  383. }  
  384.   
  385. void MainWindow::button_is_click()  
  386. {  
  387.     bool ok;  
  388.   
  389.     //单个数字,无运算结果  
  390.     if(num2 == false)  
  391.     {  
  392.         number = temp.toDouble(&ok);  
  393.         QString str1 = QString::number(number);  
  394.         resultTextEdit->setText(str1);  
  395.         return;  
  396.     }  
  397.     operation();  
  398.   
  399.     num2 = false;  
  400.     operator1 = 0;  
  401.   
  402.     if(true == flag)  
  403.     {  
  404.         resultTextEdit->setText("分母不能为0");  
  405.         return;  
  406.     }  
  407.     temp = QString::number(result);         //double转qstring  
  408.     resultTextEdit->setText(temp);  
  409. }  
  410.   
  411. void MainWindow::button_cls_click()  
  412. {  
  413.     temp = "";  
  414.     number = 0;  
  415.     operator1 = 0;  
  416.     result = 0;  
  417.     showTextEdit->setText(temp);  
  418.     resultTextEdit->setText(temp);  
  419. }  
  420.   
  421. void MainWindow::button_ce_click()  
  422. {  
  423.     temp = "";  
  424.     showTextEdit->setText(temp);  
  425. }  
  426.   
  427. void MainWindow::button_back_click()  
  428. {  
  429.     temp.chop(1);                           //chop从字符串末尾删除1个字符  
  430.     showTextEdit->setText(temp);  
  431. }  
  432.   
  433. void MainWindow::button_point_click()  
  434. {  
  435.     if(0 == temp.contains("."))  
  436.     {  
  437.         temp += ".";  
  438.     }  
  439.     showTextEdit->setText(temp);  
  440. }  
  441.   
  442. void MainWindow::button_sign_click()  
  443. {  
  444.     //判断是否位负数  
  445.     if(temp.startsWith("-"))  
  446.     {  
  447.         temp = temp.mid(1);  
  448.         showTextEdit->setText(temp);  
  449.         return;  
  450.     }  
  451.     temp = temp.insert(0, "-");  
  452.     showTextEdit->setText(temp);  
  453. }  
  454.   
  455. void MainWindow::keyPressEvent(QKeyEvent *k)  
  456. {  
  457.     //int b = k->key();  
  458.     switch(k->key())  
  459.     {  
  460.     case Qt::Key_0:button_0_click();break;  
  461.     case Qt::Key_1:button_1_click();break;  
  462.     case Qt::Key_2:button_2_click();break;  
  463.     case Qt::Key_3:button_3_click();break;  
  464.     case Qt::Key_4:button_4_click();break;  
  465.     case Qt::Key_5:button_5_click();break;  
  466.     case Qt::Key_6:button_6_click();break;  
  467.     case Qt::Key_7:button_7_click();break;  
  468.     case Qt::Key_8:button_8_click();break;  
  469.     case Qt::Key_9:button_9_click();break;  
  470.     case Qt::Key_Plus:button_add_click();break;         // +  
  471.     case Qt::Key_Minus:button_sub_click();break;        // -  
  472.     case Qt::Key_Asterisk:button_mul_click();break;     // *  
  473.     case Qt::Key_Slash:button_divide_click();break;     // /  
  474.     case Qt::Key_Backspace:button_back_click();break;   // ←  
  475.     case Qt::Key_Enter:button_is_click();break;         // =  
  476.     case Qt::Key_Period:button_point_click();break;     // .  
  477.     default:break;  
  478.     }  
  479. }  


main.cpp:

 

#include "calcMainWindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.setWindowTitle("灰熊问题的最优解");
    w.show();

    return a.exec();
}

 

posted @ 2018-05-06 22:26  sky20080101  阅读(299)  评论(0)    收藏  举报