Qt下的简易计算器项目

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt下的简易计算器项目相关的知识,希望对你有一定的参考价值。

Qt是一个比较强大的跨平台软件,首次认真学习Qt就拿计算机项目来入门。

在Qt中创建好项目,在设计模式下利用Qt的表格布局,设计好界面

技术分享

其中可以利用样式表改动界面属性

技术分享

利用Qt中的信号与槽编写功能程序

  1 #include "mainwindow.h"
  2 #include "ui_mainwindow.h"
  3 
  4 MainWindow::MainWindow(QWidget *parent) :
  5     QMainWindow(parent),
  6     ui(new Ui::MainWindow)
  7 {
  8     ui->setupUi(this);
  9     //this->setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint);
 10     setFixedSize(356,428);
 11 
 12     connect(ui->b_0,SIGNAL(clicked()),this,SLOT(handleNumClick()));
 13     connect(ui->b_1,SIGNAL(clicked()),this,SLOT(handleNumClick()));
 14     connect(ui->b_2,SIGNAL(clicked()),this,SLOT(handleNumClick()));
 15     connect(ui->b_3,SIGNAL(clicked()),this,SLOT(handleNumClick()));
 16     connect(ui->b_4,SIGNAL(clicked()),this,SLOT(handleNumClick()));
 17     connect(ui->b_5,SIGNAL(clicked()),this,SLOT(handleNumClick()));
 18     connect(ui->b_6,SIGNAL(clicked()),this,SLOT(handleNumClick()));
 19     connect(ui->b_7,SIGNAL(clicked()),this,SLOT(handleNumClick()));
 20     connect(ui->b_8,SIGNAL(clicked()),this,SLOT(handleNumClick()));
 21     connect(ui->b_9,SIGNAL(clicked()),this,SLOT(handleNumClick()));
 22 
 23     connect(ui->b_div,SIGNAL(clicked()),this,SLOT(handleOpeClick()));
 24     connect(ui->b_mod,SIGNAL(clicked()),this,SLOT(handleOpeClick()));
 25     connect(ui->b_multi,SIGNAL(clicked()),this,SLOT(handleOpeClick()));
 26     connect(ui->b_plus,SIGNAL(clicked()),this,SLOT(handleOpeClick()));
 27     connect(ui->b_sub,SIGNAL(clicked()),this,SLOT(handleOpeClick()));
 28 
 29     connect(ui->b_dot,SIGNAL(clicked()),this,SLOT(handleDotClick()));
 30     connect(ui->b_clear,SIGNAL(clicked()),this,SLOT(handleClearClick()));
 31     connect(ui->b_signed,SIGNAL(clicked()),this,SLOT(handleSignedClick()));
 32 
 33     connect(ui->b_equ,SIGNAL(clicked()),this,SLOT(handleEquClick()));
 34     m_ope1="0";
 35     m_ope2="0";
 36     m_result="0";
 37     m_step=0;
 38     m_type=0;
 39 }
 40 
 41 void MainWindow::handleClearClick()
 42 {
 43     m_ope1="0";
 44     m_ope2="0";
 45     m_result="0";
 46     m_step=0;
 47     m_type=0;
 48     ui->l_result->setText(m_result);
 49 }
 50 
 51 void MainWindow::handleDotClick()
 52 {
 53     if(m_step==0)
 54     {
 55         m_ope1+=".";
 56         ui->l_result->setText(m_ope1);
 57     }
 58     else
 59     {
 60         m_ope2+=".";
 61         ui->l_result->setText(m_ope2);
 62     }
 63 }
 64 
 65 void MainWindow::handleNumClick()
 66 {
 67     QObject *tmpObj=sender();  //定义一个指向发送信号的对象的指针
 68     QString tmpStr=tmpObj->objectName();//获取发送信号的对象的名字
 69     tmpStr=tmpStr.remove("b_");//去除b_,这样得到的是数字按钮代表的数字
 70     if(m_step==0)
 71     {
 72         if(m_ope1 =="0")
 73         {
 74             m_ope1=tmpStr;
 75         }
 76         else
 77         {    m_ope1+=tmpStr;}
 78         ui->l_result->setText(m_ope1);
 79     }
 80     else
 81     {
 82         if(m_ope2 =="0")
 83         {
 84             m_ope2=tmpStr;
 85         }
 86         else
 87         {    m_ope2+=tmpStr;}
 88         ui->l_result->setText(m_ope2);
 89     }
 90 
 91 }
 92 
 93 void MainWindow::handleEquClick()
 94 {
 95     double ope1,ope2;
 96     ope1=m_ope1.toDouble(); //Qstring类型转double类型
 97     ope2=m_ope2.toDouble();
 98     double tmpResult;
 99     switch(m_type)
100     {
101     case 0:
102         tmpResult=ope1+ope2;
103         break;
104     case 1:
105         tmpResult=ope1-ope2;
106         break;
107     case 2:
108         tmpResult=ope1*ope2;
109         break;
110     case 3:
111         tmpResult=ope1/ope2;break;
112     case 4:
113         if(!m_ope1.contains(".")&&!m_ope2.contains("."))
114             tmpResult=(int)ope1%(int)ope2;
115         break;
116 
117     }
118     m_result=QString::number(tmpResult); //double类型转Qstring类型
119     ui->l_result->setText(m_result);
120     m_ope1="0";
121     m_ope2="0";
122     m_result="0";
123     m_step=0;
124     m_type=0;
125 }
126 
127 void MainWindow::handleOpeClick()
128 {
129     m_step=1;
130     QObject *tmpObj=sender();
131     QString tmpStr=tmpObj->objectName();
132     if(tmpStr=="b_plus")
133     {
134         m_type=0;
135 
136     } if(tmpStr=="b_sub")
137     {
138         m_type=1;
139     } if(tmpStr=="b_multi")
140     {
141         m_type=2;
142     } if(tmpStr=="b_div")
143     {
144         m_type=3;
145     }
146     if(tmpStr=="b_mod")
147     {
148         m_type=4;
149     }
150 }
151 
152 void MainWindow::handleSignedClick()
153 {
154     if(m_step==0)
155     {
156         if(m_ope1[0]==-)
157             m_ope1=m_ope1.remove("-");
158         else
159         {m_ope1=m_ope1.insert(0,"-");}
160         ui->l_result->setText(m_ope1);
161     }else
162     {
163         if(m_ope2[0]==-)
164             m_ope2=m_ope2.remove("-");
165         else
166         {m_ope2=m_ope2.insert(0,"-");}
167          ui->l_result->setText(m_ope2);
168     }
169 
170 }
171 
172 MainWindow::~MainWindow()
173 {
174     delete ui;
175 }

最终得到结果

技术分享

 

以上是关于Qt下的简易计算器项目的主要内容,如果未能解决你的问题,请参考以下文章

简易Qt图片查看器

Qt简易计算器

QT 实用代码片段

项目——简易计算器

Qt编写的项目作品12-简易视频播放器

Android Studio 制作简易计算器