版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、QT編程實(shí)現(xiàn)簡易的計(jì)算器一、 實(shí)驗(yàn)?zāi)康?) 熟悉QtCreator的簡單操作。 2) 了解Qt程序編寫框架。 3) 了解信號和槽機(jī)制,熟練掌握信號與槽在應(yīng)用程序中的使用。 二、 實(shí)驗(yàn)內(nèi)容 1) 查看API手冊,學(xué)習(xí)簡單的Qt類的使用,如QLineEdit、QPushButton等。 2) 用QtCreator創(chuàng)建工程,用Qt編寫計(jì)算器程序。 3) 對計(jì)算器程序進(jìn)行移植。 三、 實(shí)驗(yàn)步驟 1. 創(chuàng)建工程 1)
2、 打開QtCreator,如圖1所示。 選擇File->New File or Project,然后在彈出的對話框中選擇Other Project->Empty Qt project(如圖2所示),然后進(jìn)入下一步。 選擇Qt版本,這里選擇使用Qt4.7.1,取消對Qt in PATH的選擇(如圖4所示),然后進(jìn)入下一步,完成新工程的創(chuàng)建(如圖5所示)。實(shí)現(xiàn)代碼Calcuulator.h代碼:#ifndef CALCULATOR_H#define CALCULAT
3、OR_H#include<QApplication>#include<QDialog>#include<QPushButton>#include<QLineEdit>class Calculator:public QDialog Q_OBJECTprotected: QLineEdit *lineEditor; QPushButton *button_0; QPushButton *button_1; QPushButton *button_2; QPushButton *button_3; QPushButton *button_4; QPus
4、hButton *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; int num1,num
5、2,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(); void button_5_clicked(); void button_6_clicked(); void button_7_clicked(
6、); 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_dengyu_clicked();#endif / CALCULATOR_HCalculator.cpp代碼:#include<QLayout&
7、gt;#include<QHBoxLayout>#include<QVBoxLayout>#include<QLineEdit>#include<cmath>#include"calculator.h"Calculator:Calculator() num1=0; num2=0; result=0; zhenghao=true; mark=1; lineEditor=new QLineEdit("0"); S="" button_0=new QPushButton("0&quo
8、t;); button_1=new QPushButton("1"); button_2=new QPushButton("2"); button_3=new QPushButton("3"); button_4=new QPushButton("4"); button_5=new QPushButton("5"); button_6=new QPushButton("6"); button_7=new QPushButton("7"); button_8
9、=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("-"); button_chu=new QPushButton("/"); button_=
10、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); Hl1->addWidget(b
11、utton_ce); Hl2->addWidget(button_1); Hl2->addWidget(button_2); Hl2->addWidget(button_3); Hl2->addWidget(button_jia); Hl3->addWidget(button_4); Hl3->addWidget(button_5); Hl3->addWidget(button_6); Hl3->addWidget(button_jian); Hl4->addWidget(button_7); Hl4->addWidget(butto
12、n_8); Hl4->addWidget(button_9); Hl4->addWidget(button_cheng); Hl5->addWidget(button_); Hl5->addWidget(button_0); Hl5->addWidget(button_dengyu); Hl5->addWidget(button_chu); QVBoxLayout *V1=new QVBoxLayout; V1->addLayout(Hl1); V1->addLayout(Hl2); V1->addLayout(Hl3); V1->a
13、ddLayout(Hl4); V1->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(); connec
14、t(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
15、_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(butt
16、on_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);void Calculator:button_0_clic
17、ked() S+="0" lineEditor->setText(S); if(mark=1) num1=num1*10+0; else num2=num2*10; void Calculator:button_1_clicked() S+="1" lineEditor->setText(S); if(mark=1) if(zhenghao) num1=num1*10+1; else num1=num1*10-1; else if(zhenghao) num2=num2*10+1; else num2=num2*10-1; void Calc
18、ulator:button_2_clicked() S+="2" lineEditor->setText(S); if(mark=1) if(zhenghao) num1=num1*10+2; else num1=num1*10-2; else if(zhenghao) num2=num2*10+2; else num2=num2*10-2; void Calculator:button_3_clicked() S+="3" lineEditor->setText(S); if(mark=1) if(zhenghao) num1=num1*1
19、0+3; else num1=num1*10-3; else if(zhenghao) num2=num2*10+3; else num2=num2*10-3; void Calculator:button_4_clicked() S+="4" lineEditor->setText(S); if(mark=1) if(zhenghao) num1=num1*10+4; else num1=num1*10-4; else if(zhenghao) num2=num2*10+4; else num2=num2*10-4; void Calculator:button_5
20、_clicked() S+="5" lineEditor->setText(S); if(mark=1) if(zhenghao) num1=num1*10+5; else num1=num1*10-5; else if(zhenghao) num2=num2*10+5; else num2=num2*10-5; void Calculator:button_6_clicked() S+="6" lineEditor->setText(S); if(mark=1) if(zhenghao) num1=num1*10+6; else num1=
21、num1*10-6; else if(zhenghao) num2=num2*10+6; else num2=num2*10-6; void Calculator:button_7_clicked() S+="7" lineEditor->setText(S); if(mark=1) if(zhenghao) num1=num1*10+7; else num1=num1*10-7; else if(zhenghao) num2=num2*10+7; else num2=num2*10-7; void Calculator:button_8_clicked() S+=&
22、quot;8" lineEditor->setText(S); if(mark=1) if(zhenghao) num1=num1*10+8; else num1=num1*10-8; else if(zhenghao) num2=num2*10+8; else num2=num2*10-8; void Calculator:button_9_clicked() S+="9" lineEditor->setText(S); if(mark=1) if(zhenghao) num1=num1*10+9; else num1=num1*10-9; else
23、 if(zhenghao) num2=num2*10+9; else num2=num2*10-9; void Calculator:button_jia_clicked() S+="+" lineEditor->setText(S); zhenghao=true; fuhao='+' mark=2;void Calculator:button_jian_clicked() S+="-" lineEditor->setText(S); zhenghao=true; fuhao='-' mark=2;void C
24、alculator:button_cheng_clicked() S+="*" lineEditor->setText(S); zhenghao=true; fuhao='*' mark=2;void Calculator:button_chu_clicked() S+="/" lineEditor->setText(S); zhenghao=true; fuhao='/' mark=2;void Calculator:button_clicked() S+="-" lineEditor->setText(S); zhenghao=false;void Calculator:button_dengyu_clicked() S+="
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025版通信網(wǎng)絡(luò)建設(shè)投標(biāo)承諾書規(guī)范范本3篇
- 能源化工行業(yè)營業(yè)員工作總結(jié)
- 人教版五年級數(shù)學(xué)上冊第3單元《小數(shù)除法》聽評課記錄
- 釣魚場租賃合同三篇
- 二零二五版私人民間借貸金錢合同抵押品管理細(xì)則3篇
- 二零二五年度企業(yè)團(tuán)建活動策劃與戶外拓展合同3篇
- 二零二五年度節(jié)能減排項(xiàng)目管理合同3篇
- 娛樂行業(yè)技術(shù)崗位總結(jié)
- 二零二五年度魚塘承包及漁村旅游開發(fā)合同2篇
- 二零二五年度公司內(nèi)部借款及資金管理協(xié)議4篇
- 城市基礎(chǔ)設(shè)施維修計(jì)劃
- 2024山西廣播電視臺招聘專業(yè)技術(shù)崗位編制人員20人歷年高頻500題難、易錯點(diǎn)模擬試題附帶答案詳解
- 新材料行業(yè)系列深度報(bào)告一:新材料行業(yè)研究框架
- 人教版小學(xué)英語各冊單詞表(帶英標(biāo))
- 廣東省潮州市潮安區(qū)2023-2024學(xué)年六年級上學(xué)期期末考試數(shù)學(xué)試題
- 餐飲業(yè)績效考核表(店長、前廳領(lǐng)班、吧臺、廚師長、后廚、服務(wù)員、收銀員、庫管、后勤)3
- 骨髓穿刺課件
- 2024中國保險發(fā)展報(bào)告-中南大風(fēng)險管理研究中心.燕道數(shù)科
- 元素的用途完整版本
- 七十歲換領(lǐng)證駕考三力測試答題
- 鄉(xiāng)村治理中正式制度與非正式制度的關(guān)系解析
評論
0/150
提交評論