您好,登錄后才能下訂單哦!
這篇文章主要講解了“QT如何實現簡單計算器功能”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“QT如何實現簡單計算器功能”吧!
效果圖:
新建工程,創建類MainWindow,基類是QMainWindow,聲明變量和函數、槽
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QPushButton> #include <QLineEdit> class MainWindow : public QMainWindow { Q_OBJECT protected: int num1,num2,result; //標記第一個數,第二個數和結果 int operat; //運算符號 int operat_flag;//標記是否輸入符號 QLineEdit *data; 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_result; QString s; public: MainWindow(QWidget *parent = 0); ~MainWindow(); public: void myLayout(); void operation(); void readNum(int num); void init_data(); 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(); void button_8_clicked(); void button_9_clicked(); void button_ce_clicked(); void button_result_clicked(); void button_cheng_clicked(); void button_chu_clicked(); void button_jia_clicked(); void button_jian_clicked(); }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include <QPushButton> #include <QLineEdit> #include <QGridLayout> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { } void MainWindow::myLayout(){ QWidget *centerWindow = new QWidget; this->setCentralWidget(centerWindow); s = ""; init_data(); data = new QLineEdit(""); button_0 = new QPushButton("0"); 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 = new QPushButton("8"); button_9 = new QPushButton("9"); button_ce = new QPushButton("CE"); button_jia = new QPushButton("+"); button_jian = new QPushButton("-"); button_cheng = new QPushButton("*"); button_chu = new QPushButton("/"); button_result = new QPushButton("="); data->setFixedHeight(30); button_0->setMinimumHeight(50); button_1->setMinimumHeight(50); button_2->setMinimumHeight(50); button_3->setMinimumHeight(50); button_4->setMinimumHeight(50); button_5->setMinimumHeight(50); button_6->setMinimumHeight(50); button_7->setMinimumHeight(50); button_8->setMinimumHeight(50); button_9->setMinimumHeight(50); button_ce->setMinimumHeight(50); button_jia->setMinimumHeight(50); button_jian->setMinimumHeight(50); button_cheng->setMinimumHeight(50); button_chu->setMinimumHeight(50); button_result->setMinimumHeight(50); //連接信號 connect(button_0,SIGNAL(clicked(bool)),this,SLOT(button_0_clicked())); connect(button_1,SIGNAL(clicked(bool)),this,SLOT(button_1_clicked())); connect(button_2,SIGNAL(clicked(bool)),this,SLOT(button_2_clicked())); connect(button_3,SIGNAL(clicked(bool)),this,SLOT(button_3_clicked())); connect(button_4,SIGNAL(clicked(bool)),this,SLOT(button_4_clicked())); connect(button_5,SIGNAL(clicked(bool)),this,SLOT(button_5_clicked())); connect(button_6,SIGNAL(clicked(bool)),this,SLOT(button_6_clicked())); connect(button_7,SIGNAL(clicked(bool)),this,SLOT(button_7_clicked())); connect(button_8,SIGNAL(clicked(bool)),this,SLOT(button_8_clicked())); connect(button_9,SIGNAL(clicked(bool)),this,SLOT(button_9_clicked())); connect(button_jia,SIGNAL(clicked(bool)),this,SLOT(button_jia_clicked())); connect(button_jian,SIGNAL(clicked(bool)),this,SLOT(button_jian_clicked())); connect(button_cheng,SIGNAL(clicked(bool)),this,SLOT(button_cheng_clicked())); connect(button_chu,SIGNAL(clicked(bool)),this,SLOT(button_chu_clicked())); connect(button_ce,SIGNAL(clicked(bool)),this,SLOT(button_ce_clicked())); connect(button_result,SIGNAL(clicked(bool)),this,SLOT(button_result_clicked())); QGridLayout * layout = new QGridLayout; layout->addWidget(data,1,1,1,4,Qt::Alignment()); layout->addWidget(button_1,2,1,Qt::Alignment()); layout->addWidget(button_2,2,2,Qt::Alignment()); layout->addWidget(button_3,2,3,Qt::Alignment()); layout->addWidget(button_jia,2,4,Qt::Alignment()); layout->addWidget(button_4,3,1,Qt::Alignment()); layout->addWidget(button_5,3,2,Qt::Alignment()); layout->addWidget(button_6,3,3,Qt::Alignment()); layout->addWidget(button_jian,3,4,Qt::Alignment()); layout->addWidget(button_7,4,1,Qt::Alignment()); layout->addWidget(button_8,4,2,Qt::Alignment()); layout->addWidget(button_9,4,3,Qt::Alignment()); layout->addWidget(button_cheng,4,4,Qt::Alignment()); layout->addWidget(button_ce,5,1,Qt::Alignment()); layout->addWidget(button_0,5,2,Qt::Alignment()); layout->addWidget(button_result,5,3,Qt::Alignment()); layout->addWidget(button_chu,5,4,Qt::Alignment()); layout->setMargin(12); centerWindow->setLayout(layout); } MainWindow::~MainWindow() { } void MainWindow::init_data() { num1 = 0; num2 = 0; operat_flag = 0; } void MainWindow::readNum(int num) { //如果是第一個操作數 if(operat_flag == 0){ num1 = num1 * 10 + num; } //如果是第二個操作數 else{ num2 = num2 * 10 + num; } } void MainWindow::operation() { switch(operat){ case 1: result = num1 + num2; break; case 2: result = num1 - num2; break; case 3: result = num1 * num2; break; case 4: result = num1 / num2; break; } //把結果存到num1中,清零num2 num1 = result; num2 = 0; s = QString::number(result,10); } void MainWindow::button_0_clicked(){ s += "0"; data->setText(s); readNum(0); } void MainWindow::button_1_clicked(){ s += "1"; data->setText(s); readNum(1); } void MainWindow::button_2_clicked(){ s += "2"; data->setText(s); readNum(2); } void MainWindow::button_3_clicked(){ s += "3"; data->setText(s); readNum(3); } void MainWindow::button_4_clicked(){ s += "4"; data->setText(s); readNum(4); } void MainWindow::button_5_clicked(){ s += "5"; data->setText(s); readNum(5); } void MainWindow::button_6_clicked(){ s += "6"; data->setText(s); readNum(6); } void MainWindow::button_7_clicked(){ s += "7"; data->setText(s); readNum(7); } void MainWindow::button_8_clicked(){ s += "8"; data->setText(s); readNum(8); } void MainWindow::button_9_clicked(){ s += "9"; data->setText(s); readNum(9); } void MainWindow::button_ce_clicked(){ s = ""; data->setText(""); //初始化數據 init_data(); } void MainWindow::button_jia_clicked(){ s += "+"; data->setText(s); operat_flag = 1; operat = 1; } void MainWindow::button_jian_clicked(){ s += "-"; data->setText(s); operat_flag = 1; operat = 2; } void MainWindow::button_cheng_clicked(){ s += "*"; data->setText(s); operat_flag = 1; operat = 3; } void MainWindow::button_chu_clicked(){ s += "/"; data->setText(s); operat_flag = 1; operat = 4; } void MainWindow::button_result_clicked(){ operation(); QString a = QString::number(result,10); data->setText(a); }
main.cpp
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); w.setFixedSize(400,300); w.setWindowTitle("計算器"); w.myLayout(); return a.exec(); }
演示:
99*100=
結果
繼續計算/99
感謝各位的閱讀,以上就是“QT如何實現簡單計算器功能”的內容了,經過本文的學習后,相信大家對QT如何實現簡單計算器功能這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。