您好,欢迎访问三七文档
Qt设计报告-----计算器实现Qt设计报告----------------计算器实现学院:xxxxxxxxxxxxxxxxxxxx姓名:xxxxxxxxxxxxxxxx学号:xxxxxxxxxxxxxxxxxxxx邮箱:xxxxxxxxxxxx@qq.com联系方式:xxxxxxxxxxxxxxxxxxxQt设计报告-----计算器实现myWidget.h文件代码如下:#ifndefMYWIDGET_H#defineMYWIDGET_H#includeqwidget.h#includeqpushbutton.h#includeqlineedit.hclassmyWidget:publicQWidget{Q_OBJECTprivate:QStringQ1,Q2;//Q1和Q2是分别用来保存操作数QStringQ3;//用于保存操作数charc;//c用于保存运算符..QLineEdit*lineEdit;//第一行按钮QPushButton*bMAdd;//保存数据按钮QPushButton*bMSub;//读取数据按钮QPushButton*bClear;//清理屏幕按钮QPushButton*bAdd;//’+‘按钮//第二行按钮QPushButton*b1;//’1‘按钮QPushButton*b2;//‘2’按钮QPushButton*b3;//’3‘按钮QPushButton*bSub;//’-‘按钮//第三行按钮QPushButton*b4;//’4‘按钮QPushButton*b5;//’5‘按钮QPushButton*b6;//’6‘按钮QPushButton*bMul;//’*‘按钮//第四行按钮QPushButton*b7;//’7‘按钮QPushButton*b8;//‘8’按钮QPushButton*b9;//‘9’按钮QPushButton*bDiv;//‘/’按钮//第五行按钮QPushButton*bPoi;//‘.’按钮QPushButton*b0;//‘0’按钮QPushButton*bEqual;//‘=’按钮Qt设计报告-----计算器实现public:myWidget(){Q1=0;Q2=0;Q3=0;c='';this-setMinimumSize(300,290);this-setMaximumSize(300,290);QWidget*editwindow=newQWidget(this);//编辑窗口editwindow-setGeometry(40,20,220,50);lineEdit=newQLineEdit(欢迎使用就算器,editwindow);//显示输入框lineEdit-setGeometry(0,0,220,50);lineEdit-setReadOnly(1);//设置为只读,不允许直接修改文本框QWidget*buttonwindow=newQWidget(this);buttonwindow-setGeometry(0,90,300,200);bMAdd=newQPushButton(M+,buttonwindow);//第一行按钮bMAdd-setGeometry(40,0,40,20);bMSub=newQPushButton(M-,buttonwindow);bMSub-setGeometry(100,0,40,20);bClear=newQPushButton(clear,buttonwindow);bClear-setGeometry(160,0,40,20);bAdd=newQPushButton(+,buttonwindow);bAdd-setGeometry(220,0,40,20);b1=newQPushButton(1,buttonwindow);//第二行按钮b1-setGeometry(40,40,40,20);b2=newQPushButton(2,buttonwindow);b2-setGeometry(100,40,40,20);b3=newQPushButton(3,buttonwindow);b3-setGeometry(160,40,40,20);bSub=newQPushButton(-,buttonwindow);bSub-setGeometry(220,40,40,20);b4=newQPushButton(4,buttonwindow);//第三行按钮b4-setGeometry(40,80,40,20);b5=newQPushButton(5,buttonwindow);b5-setGeometry(100,80,40,20);b6=newQPushButton(6,buttonwindow);b6-setGeometry(160,80,40,20);bMul=newQPushButton(*,buttonwindow);bMul-setGeometry(220,80,40,20);Qt设计报告-----计算器实现b7=newQPushButton(7,buttonwindow);//第四行按钮b7-setGeometry(40,120,40,20);b8=newQPushButton(8,buttonwindow);b8-setGeometry(100,120,40,20);b9=newQPushButton(9,buttonwindow);b9-setGeometry(160,120,40,20);bDiv=newQPushButton(/,buttonwindow);bDiv-setGeometry(220,120,40,20);b0=newQPushButton(0,buttonwindow);//第四行按钮b0-setGeometry(40,160,40,20);bPoi=newQPushButton(.,buttonwindow);bPoi-setGeometry(100,160,40,20);bEqual=newQPushButton(=,buttonwindow);bEqual-setGeometry(160,160,40,20);//信号连接connect(bClear,SIGNAL(clicked()),this,SLOT(sClear()));connect(b1,SIGNAL(clicked()),this,SLOT(s1()));connect(b2,SIGNAL(clicked()),this,SLOT(s2()));connect(b3,SIGNAL(clicked()),this,SLOT(s3()));connect(b4,SIGNAL(clicked()),this,SLOT(s4()));connect(b5,SIGNAL(clicked()),this,SLOT(s5()));connect(b6,SIGNAL(clicked()),this,SLOT(s6()));connect(b7,SIGNAL(clicked()),this,SLOT(s7()));connect(b8,SIGNAL(clicked()),this,SLOT(s8()));connect(b9,SIGNAL(clicked()),this,SLOT(s9()));connect(b0,SIGNAL(clicked()),this,SLOT(s0()));connect(bAdd,SIGNAL(clicked()),this,SLOT(sAdd()));connect(bSub,SIGNAL(clicked()),this,SLOT(sSub()));connect(bMul,SIGNAL(clicked()),this,SLOT(sMul()));connect(bDiv,SIGNAL(clicked()),this,SLOT(sDiv()));connect(bPoi,SIGNAL(clicked()),this,SLOT(sPoi()));connect(bEqual,SIGNAL(clicked()),this,SLOT(sEqual()));connect(bMAdd,SIGNAL(clicked()),this,SLOT(sMAdd()));connect(bMSub,SIGNAL(clicked()),this,SLOT(sMSub()));}publicslots://自定义槽voids1()//数字按键“1”的槽函数1-9同理{if(Q2==0)Q2=1;//如果Q2为0替代0else//如果不是,获取当前文本编辑框内容,按一下按钮在文本框内容Qt设计报告-----计算器实现后追加1{Q2=lineEdit-text();Q2.append('1');}lineEdit-setText(Q2);//将q2在文本编辑框中显示}voids2(){if(Q2==0)Q2=2;else{Q2=lineEdit-text();Q2.append('2');}lineEdit-setText(Q2);}voids3(){if(Q2==0)Q2=3;else{Q2=lineEdit-text();Q2.append('3');}lineEdit-setText(Q2);}voids4(){if(Q2==0)Q2=4;else{Q2=lineEdit-text();Q2.append('4');}lineEdit-setText(Q2);}voids5(){if(Q2==0)Q2=5;else{Q2=lineEdit-text();Q2.append('5');Qt设计报告-----计算器实现}lineEdit-setText(Q2);}voids6(){if(Q2==0)Q2=6;else{Q2=lineEdit-text();Q2.append('6');}lineEdit-setText(Q2);}voids7(){if(Q2==0)Q2=7;else{Q2=lineEdit-text();Q2.append('7');}lineEdit-setText(Q2);}voids8(){if(Q2==0)Q2=8;else{Q2=lineEdit-text();Q2.append('8');}lineEdit-setText(Q2);}voids9(){if(Q2==0)Q2=9;else{Q2=lineEdit-text();Q2.append('9');}lineEdit-setText(Q2);}voids0()Qt设计报告-----计算器实现{if(Q2==0)Q2=0;else{Q2=lineEdit-text();Q2.append('0');}lineEdit-setText(Q2);//将q2在文本编辑框中显示}//定义按钮0—9槽voidsPoi()//小数点槽{if(Q2==0)Q2=0.;else{Q2=lineEdit-text();Q2.append('.');}lineEdit-setText(Q2);}voidsAdd()//加号槽{doublen,m;Q2=lineEdit-text();if(c=='')//如果c为空格键,说明现在是第一次运算,那么不进行任何操作,只是将Q2复制给Q1同时保存这一次的操作符,以便下一次计算.{Q1=Q2;Q2=0;c='+';}else{n=Q1.toDouble();//如果上一次已经有运算符,那么将Q1,Q2都转换成浮点数m=Q2.toDouble();if(c=='+')n=n+m;if(c=='-')n=n-m;if(c=='*')n=n*m;if(c=='/')n=n/m;//实现运算Q1=QString::number(n,'f',10);//再转换回字符形式Q2=0;//将Q2重新初始化c=
本文标题:Qt实现计算器
链接地址:https://www.777doc.com/doc-5958092 .html