qt编程简单的计算器Word文件下载.docx
《qt编程简单的计算器Word文件下载.docx》由会员分享,可在线阅读,更多相关《qt编程简单的计算器Word文件下载.docx(15页珍藏版)》请在冰豆网上搜索。
project(如图2所示),然后进入下一步。
选择Qt版本,这里选择使用Qt4.7.1,取消对Qt
in
PATH的选择(如图4所示),然后进入下一步,完成新工程的创建(如图5所示)。
实现代码
Calcuulator.h代码:
#ifndefCALCULATOR_H
#defineCALCULATOR_H
#include<
QApplication>
QDialog>
QPushButton>
QLineEdit>
classCalculator:
publicQDialog
{
Q_OBJECT
protected:
QLineEdit*lineEditor;
QPushButton*button_0;
QPushButton*button_1;
QPushButton*button_2;
QPushButton*button_3;
QPushButton*button_4;
QPushButton*button_5;
QPushButton*button_6;
QPushButton*button_7;
QPushButton*button_8;
QPushButton*button_9;
QPushButton*button_ce;
QPushButton*button_jia;
QPushButton*button_jian;
QPushButton*button_cheng;
QPushButton*button_chu;
QPushButton*button__;
QPushButton*button_dengyu;
intnum1,num2,result;
//
boolzhenghao;
intmark;
charfuhao;
QStringS;
public:
Calculator();
privateslots:
voidbutton_0_clicked();
voidbutton_1_clicked();
voidbutton_2_clicked();
voidbutton_3_clicked();
voidbutton_4_clicked();
voidbutton_5_clicked();
voidbutton_6_clicked();
voidbutton_7_clicked();
voidbutton_8_clicked();
voidbutton_9_clicked();
voidbutton_ce_clicked();
voidbutton_jia_clicked();
voidbutton_jian_clicked();
voidbutton_cheng_clicked();
voidbutton_chu_clicked();
voidbutton__clicked();
voidbutton_dengyu_clicked();
};
#endif//CALCULATOR_H
Calculator.cpp代码:
QLayout>
QHBoxLayout>
QVBoxLayout>
cmath>
#include"
calculator.h"
Calculator:
:
Calculator()
num1=0;
num2=0;
result=0;
zhenghao=true;
mark=1;
lineEditor=newQLineEdit("
0"
);
S="
"
;
button_0=newQPushButton("
button_1=newQPushButton("
1"
button_2=newQPushButton("
2"
button_3=newQPushButton("
3"
button_4=newQPushButton("
4"
button_5=newQPushButton("
5"
button_6=newQPushButton("
6"
button_7=newQPushButton("
7"
button_8=newQPushButton("
8"
button_9=newQPushButton("
9"
button_ce=newQPushButton("
CE"
button_cheng=newQPushButton("
*"
button_jia=newQPushButton("
+"
button_jian=newQPushButton("
-"
button_chu=newQPushButton("
/"
button__=newQPushButton("
+/-"
button_dengyu=newQPushButton("
="
QHBoxLayout*Hl1=newQHBoxLayout;
QHBoxLayout*Hl2=newQHBoxLayout;
QHBoxLayout*Hl3=newQHBoxLayout;
QHBoxLayout*Hl4=newQHBoxLayout;
QHBoxLayout*Hl5=newQHBoxLayout;
Hl1->
addWidget(lineEditor);
addWidget(button_ce);
Hl2->
addWidget(button_1);
addWidget(button_2);
addWidget(button_3);
addWidget(button_jia);
Hl3->
addWidget(button_4);
addWidget(button_5);
addWidget(button_6);
addWidget(button_jian);
Hl4->
addWidget(button_7);
addWidget(button_8);
addWidget(button_9);
addWidget(button_cheng);
Hl5->
addWidget(button__);
addWidget(button_0);
addWidget(button_dengyu);
addWidget(button_chu);
QVBoxLayout*V1=newQVBoxLayout;
V1->
addLayout(Hl1);
addLayout(Hl2);
addLayout(Hl3);
addLayout(Hl4);
addLayout(Hl5);
connect(button_0,SIGNAL(clicked()),this,SLOT(button_0_clicked()));
connect(button_1,SIGNAL(clicked()),this,SLOT(button_1_clicked()));
connect(button_2,SIGNAL(clicked()),this,SLOT(button_2_clicked()));
connect(button_3,SIGNAL(clicked()),this,SLOT(button_3_clicked()));
connect(button_4,SIGNAL(clicked()),this,SLOT(button_4_clicked()));
connect(button_5,SIGNAL(clicked()),this,SLOT(button_5_clicked()));
connect(button_6,SIGNAL(clicked()),this,SLOT(button_6_clicked()));
connect(button_7,SIGNAL(clicked()),this,SLOT(button_7_clicked()));
connect(button_8,SIGNAL(clicked()),this,SLOT(button_8_clicked()));
connect(button_9,SIGNAL(clicked()),this,SLOT(button_9_clicked()));
connect(button_jia,SIGNAL(clicked()),this,SLOT(button_jia_clicked()));
connect(button_jian,SIGNAL(clicked()),this,SLOT(button_jian_clicked()));
connect(button_cheng,SIGNAL(clicked()),this,SLOT(button_cheng_clicked()));
connect(button_chu,SIGNAL(clicked()),this,SLOT(button_chu_clicked()));
connect(button_dengyu,SIGNAL(clicked()),this,SLOT(button_dengyu_clicked()));
connect(button_ce,SIGNAL(clicked()),this,SLOT(button_ce_clicked()));
connect(button__,SIGNAL(clicked()),this,SLOT(button__clicked()));
setLayout(V1);
}
voidCalculator:
button_0_clicked()
S+="
lineEditor->
setText(S);
if(mark==1){
num1=num1*10+0;
}else{
num2=num2*10;
}
button_1_clicked(){
if(zhenghao){
num1=num1*10+1;
num1=num1*10-1;
num2=num2*10+1;
num2=num2*10-1;
button_2_clicked(){
num1=num1*10+2;
num1=num1*10-2;
num2=num2*10+2;
num2=num2*10-2;
button_3_clicked(){
num1=num1*10+3;
num1=num1*10-3;
num2=num2*10+3;
num2=num2*10-3;
button_4_clicked(){
num1=num1*10+4;
num1=num1*10-4;
num2=num2*10+4;
num2=num2*10-4;
button_5_clicked(){
num1=num1*10+5;
num1=num1*10-5;
num2=num2*10+5;
num2=num2*10-5;
button_6_clicked(){
num1=num1*10+6;
num1=num1*10-6;
num2=num2*10+6;
num2=num2*10-6;
button_7_clicked(){
num1=num1*10+7;
num1=num1*10-7;
num2=num2*10+7;
num2=num2*10-7;
button_8_clicked(){
num1=num1*10+8;
num1=num1*10-8;
num2=num2*10+8;
num2=num2*10-8;
button_9_clicked(){
num1=num1*10+9;
num1=num1*10-9;
num2=num2*10+9;
num2=num2*10-9;
button_jia_clicked(){
fuhao='
+'
mark=2;
button_jian_clicked(){
-'
button_cheng_clicked(){
*'
button_chu_clicked(){
/'
button__clicked(){
zhenghao=false;
button_dengyu_clicked(){
//lineEditor->
switch(fuhao){
case'
result=num1+num2;
break;
result=num1-num2;
result=num1*num2;
if(num2)result=num1/num2;
inti=0;
intresult1=result;
while(result1){
i++;
result1/=10;
for(;
i;
i--){
S+=(result/(int)pow(10,i-1)+'
0'
result%=(int)pow(10,i-1);
num1=num2=0;
button_ce_clicked()
setText("
Main代码:
intmain(intargc,char*argv[])
QApplicationapp(argc,argv);
Calculatorcal;
cal.show();
returnapp.exec();
运行结果
四、总结
通过计算器设计,学到了利用Qt设计简单窗口应用程序的的方法,初步了解了使用QT工具编写窗口程序的新颖,了解了槽和信号等基本知识,学会了如何使用新工具(Qt)帮助程序开发。