1、project(如图2所示),然后进入下一步。选择Qt版本,这里选择使用Qt4.7.1,取消对QtinPATH的选择(如图4所示),然后进入下一步,完成新工程的创建(如图5所示)。实现代码Calcuulator.h代码:#ifndef CALCULATOR_H#define CALCULATOR_H#includeQDialogQPushButtonQLineEditclass Calculator:public QDialog Q_OBJECTprotected: QLineEdit *lineEditor; QPushButton *button_0; QPushButton *butto
2、n_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; QPushButt
3、on *button_chu; QPushButton *button_; QPushButton *button_dengyu; int num1,num2,result; / bool zhenghao; int mark; char fuhao; QString S;public: Calculator();private slots: void button_0_clicked(); void button_1_clicked(); void button_2_clicked(); void button_3_clicked(); void button_4_clicked(); vo
4、id button_5_clicked(); void button_6_clicked(); void button_7_clicked(); void button_8_clicked(); void button_9_clicked(); void button_ce_clicked(); void button_jia_clicked(); void button_jian_clicked(); void button_cheng_clicked(); void button_chu_clicked(); void button_clicked(); void button_dengy
5、u_clicked();#endif / CALCULATOR_HCalculator.cpp代码:QLayoutQHBoxLayoutQVBoxLayoutcmath#includecalculator.hCalculator:Calculator() num1=0; num2=0; result=0; zhenghao=true; mark=1; lineEditor=new QLineEdit(0); S=; button_0=new QPushButton( button_1=new QPushButton(1 button_2=new QPushButton(2 button_3=n
6、ew QPushButton(3 button_4=new QPushButton(4 button_5=new QPushButton(5 button_6=new QPushButton(6 button_7=new QPushButton(7 button_8=new QPushButton(8 button_9=new QPushButton(9 button_ce=new QPushButton(CE button_cheng=new QPushButton(* button_jia=new QPushButton(+ button_jian=new QPushButton(- bu
7、tton_chu=new QPushButton(/ button_=new QPushButton(+/- button_dengyu=new QPushButton(= QHBoxLayout *Hl1=new QHBoxLayout; QHBoxLayout *Hl2=new QHBoxLayout; QHBoxLayout *Hl3=new QHBoxLayout; QHBoxLayout *Hl4=new QHBoxLayout; QHBoxLayout *Hl5=new QHBoxLayout; Hl1-addWidget(lineEditor);addWidget(button_
8、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_);addWidge
9、t(button_0);addWidget(button_dengyu);addWidget(button_chu); QVBoxLayout *V1=new QVBoxLayout; 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();
10、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(
11、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,
12、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(),t
13、his,SLOT(button_ce_clicked(); connect(button_,SIGNAL(clicked(),this,SLOT(button_clicked(); setLayout(V1);void Calculator: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=nu
14、m2*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*1
15、0-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()
16、 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
17、=num1*num2;if(num2)result=num1/num2; int i=0; int result1=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代码:int main(int argc,char *argv) QApplication app(argc,argv); Calculator cal; cal.show(); return app.exec();运行结果四、总结通过计算器设计,学到了利用Qt设计简单窗口应用程序的的方法,初步了解了使用QT工具编写窗口程序的新颖,了解了槽和信号等基本知识,学会了如何使用新工具(Qt)帮助程序开发。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1