QT学习 之 计算器的实现
前些天剛開始接觸QT,對QT的開發環境有了些許了解。覺得用QT寫一個軟件還是很方便的。當然由于0基礎遇到的問題也不少。
1.對于QT學習方法還在探究之中,目前想到的,認為比較有效的方法就是先看教學視頻,然后試著編寫一些自己感興趣的小軟件。通過軟件去查漏補缺。(希望大佬們推薦一下更有效的學習方法)。
2.當我寫完一個計算器程序以后,我想在我的根目錄里面去運行 *.exe 文件,發現根本不能運行,他告訴我缺少某個.dll文件。然后我又按照視頻里面的方法去修改了環境變量,還真的有效。然后我把程序拿到女朋友的電腦去運行,系統又提示我缺少某個 .dll 文件。然后我又去查了下資料,說什么QT是動態編譯的,需要添加他所包含的一系列 *.dll文件。隨后就去添加了各種 *.dll 文件。添加完了以后,還是不能運行,又去查,說有些需要包含的文件并不會顯示出來。接著 我又發現了 ?windeployqt ?。這東西是真的好用,他直接把你運行的程序所包含的 .dll文件直接全部復制到你的根目錄下。這樣就省去了自己復制的步驟。
3.雖然程序在他人的電腦上可以運行了,但是包含的東西太多,軟件太大了。然后在好心網友的知道下,知道了 inno setup 這個軟件,它能直接打包你需要運行的程序和他附屬的各種文件。至此,我的簡便計算器軟件,就完全寫好了。。。。。。。。。。。。。。。。
下面上傳我的代碼。
#ifndef MAINWINDOW_H #define MAINWINDOW_H#include <QMainWindow> #include <QString> #include <QMessageBox> // 消息提示框namespace Ui { class MainWindow; }class MainWindow : public QMainWindow {Q_OBJECTpublic:explicit MainWindow(QWidget *parent = 0);~MainWindow();private:Ui::MainWindow *ui; //建立0-9的槽 private slots:void zeroslotclicked(); private slots:void oneslotclicked(); private slots:void twoslotclicked(); private slots:void threeslotclicked(); private slots:void fourslotclicked(); private slots:void fiveslotclicked(); private slots:void sixslotclicked(); private slots:void sevenslotclicked(); private slots:void eightslotclicked(); private slots:void nineslotclicked(); //建立加減乘除等于點的槽 private slots:void addslotclicked(); private slots:void subtractslotclicked(); private slots:void rideslotclicked(); private slots:void divideslotclicked(); private slots:void equalslotclicked(); private slots:void decimalslotclicked(); private slots:void cslotclicked();};#endif // MAINWINDOW_H #include "mainwindow.h" #include <QApplication> #include <QString> int main(int argc, char *argv[]) {QApplication a(argc, argv);MainWindow w;w.show();return a.exec(); }
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QString>MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow){ui->setupUi(this);this->setMaximumSize(537,410); //設置為固定的大小this->setMinimumSize(537,410);// 0-9 .QObject::connect(ui->zero,SIGNAL(clicked()),this,SLOT(zeroslotclicked()));QObject::connect(ui->one,SIGNAL(clicked()),this,SLOT(oneslotclicked()));QObject::connect(ui->two,SIGNAL(clicked()),this,SLOT(twoslotclicked()));QObject::connect(ui->three,SIGNAL(clicked()),this,SLOT(threeslotclicked()));QObject::connect(ui->four,SIGNAL(clicked()),this,SLOT(fourslotclicked()));QObject::connect(ui->five,SIGNAL(clicked()),this,SLOT(fiveslotclicked()));QObject::connect(ui->six,SIGNAL(clicked()),this,SLOT(sixslotclicked()));QObject::connect(ui->seven,SIGNAL(clicked()),this,SLOT(sevenslotclicked()));QObject::connect(ui->eight,SIGNAL(clicked()),this,SLOT(eightslotclicked()));QObject::connect(ui->nine,SIGNAL(clicked()),this,SLOT(nineslotclicked()));QObject::connect(ui->decimal,SIGNAL(clicked()),this,SLOT(decimalslotclicked()));// 加減乘除等QObject::connect(ui->add,SIGNAL(clicked()),this,SLOT(addslotclicked()));QObject::connect(ui->subtract,SIGNAL(clicked()),this,SLOT(subtractslotclicked()));QObject::connect(ui->ride,SIGNAL(clicked()),this,SLOT(rideslotclicked()));QObject::connect(ui->divide,SIGNAL(clicked()),this,SLOT(divideslotclicked()));QObject::connect(ui->equal,SIGNAL(clicked()),this,SLOT(equalslotclicked()));//復位QObject::connect(ui->c,SIGNAL(clicked()),this,SLOT(cslotclicked()));}MainWindow::~MainWindow() {delete ui; } //獲取0-9 Button的值 double flag,counter=1.000000,result,firstnum,secondnum,thirdnum,fourthnum; char midresult; void MainWindow::zeroslotclicked() {if(flag==1){counter=counter*10.0000;result=result+0.000000/counter;}else{result=result*10+0;}ui->ResultEdit->setText(QString::number(result)); } void MainWindow::oneslotclicked() {if(flag==1){counter=counter*10.0000;result=result+1.000000/counter;}else{result=result*10+1;}ui->ResultEdit->setText(QString::number(result)); } void MainWindow::twoslotclicked() {if(flag==1){counter=counter*10.0000;result=result+2.000000/counter;}else{result=result*10+2;}ui->ResultEdit->setText(QString::number(result)); } void MainWindow::threeslotclicked() {if(flag==1){counter=counter*10.0000;result=result+3.000000/counter;}else{result=result*10+3;}ui->ResultEdit->setText(QString::number(result)); } void MainWindow::fourslotclicked() {if(flag==1){counter=counter*10.0000;result=result+4.000000/counter;}else{result=result*10+4;}ui->ResultEdit->setText(QString::number(result)); } void MainWindow::fiveslotclicked() {if(flag==1){counter=counter*10.0000;result=result+5.000000/counter;}else{result=result*10+5;}ui->ResultEdit->setText(QString::number(result)); } void MainWindow::sixslotclicked() {if(flag==1){counter=counter*10.0000;result=result+6.000000/counter;}else{result=result*10+6;}ui->ResultEdit->setText(QString::number(result)); } void MainWindow::sevenslotclicked() {if(flag==1){counter=counter*10.0000;result=result+7.000000/counter;}else{result=result*10+7;}ui->ResultEdit->setText(QString::number(result)); } void MainWindow::eightslotclicked() {if(flag==1){counter=counter*10.0000;result=result+8.000000/counter;}else{result=result*10+8;}ui->ResultEdit->setText(QString::number(result)); } void MainWindow::nineslotclicked() {if(flag==1){counter=counter*10.000;result=result+9.00000/counter;}else{result=result*10+9;}ui->ResultEdit->setText(QString::number(result)); }// 顯示加減乘除等于void MainWindow::decimalslotclicked() {flag=1;ui->ResultEdit->setText(QString::number(result)+"."); }void MainWindow::addslotclicked() {flag=0;firstnum=result;result=0;counter=1.000000;midresult='+';ui->ResultEdit->setText("+"); }void MainWindow::subtractslotclicked() {flag=0;firstnum=result;result=0;counter=1.0000000;midresult='-';ui->ResultEdit->setText("-"); } void MainWindow::rideslotclicked() {flag=0;firstnum=result;result=0;counter=1.000000;midresult='*';ui->ResultEdit->setText("*"); }void MainWindow::divideslotclicked() {flag=0;firstnum=result;result=0;counter=1.000000;midresult='/';ui->ResultEdit->setText("/"); }void MainWindow::equalslotclicked() {flag=0;secondnum=result; //判斷運算法則,然后進行相應的運算;if(midresult=='+'){result=firstnum+secondnum;}if(midresult=='-'){result=firstnum-secondnum;}if(midresult=='*'){result=firstnum*secondnum;}if(midresult=='/'){if(secondnum==0){QMessageBox::warning(this,"Error Message","Error");return;}result=firstnum/secondnum;}ui->ResultEdit->setText(QString::number(result));result=0;counter=1.000000; }void MainWindow::cslotclicked() {flag=0;firstnum=0;secondnum=0;result=0;counter=1.000000;midresult='C';ui->ResultEdit->setText("0"); }
總結
以上是生活随笔為你收集整理的QT学习 之 计算器的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2017.8.17 开始了我的QT 学
- 下一篇: QT 快捷键整理