基于QT5.15.2 在 Qt Creator 中开发
1、首先设计一个简单的计算器UI
2、代码部分
mainwindow.h的代码
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class mainwindow; }
QT_END_NAMESPACE
class mainwindow : public QWidget
{
Q_OBJECT
public:
mainwindow(QWidget *parent = nullptr);
~mainwindow();
// 槽函数的声明
private slots:
void number_clicked();
void operator_clicked();
void equal_clicked();
void clear_clicked();
private:
Ui::mainwindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp 的代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QDebug>
double ans = 0.0;
bool addFlag = false;
bool subFlag = false;
bool mulFlag = false;
bool divFlag = false;
mainwindow::mainwindow(QWidget *parent)
: QWidget(parent)
, ui(new Ui::mainwindow)
{
ui->setupUi(this);
// 进行信号和槽的连接
connect(ui->pushButton_0, SIGNAL(clicked(bool)), this, SLOT(number_clicked()));
connect(ui->pushButton_1, SIGNAL(clicked(bool)), this, SLOT(number_clicked()));
connect(ui->pushButton_2, SIGNAL(clicked(bool)), this, SLOT(number_clicked()));
connect(ui->pushButton_3, SIGNAL(clicked(bool)), this, SLOT(number_clicked()));
connect(ui->pushButton_4, SIGNAL(clicked(bool)), this, SLOT(number_clicked()));
connect(ui->pushButton_5, SIGNAL(clicked(bool)), this, SLOT(number_clicked()));
connect(ui->pushButton_6, SIGNAL(clicked(bool)), this, SLOT(number_clicked()));
connect(ui->pushButton_7, SIGNAL(clicked(bool)), this, SLOT(number_clicked()));
connect(ui->pushButton_8, SIGNAL(clicked(bool)), this, SLOT(number_clicked()));
connect(ui->pushButton_9, SIGNAL(clicked(bool)), this, SLOT(number_clicked()));
connect(ui->pushButton_add, SIGNAL(clicked(bool)), this, SLOT(operator_clicked()));
connect(ui->pushButton_sub, SIGNAL(clicked(bool)), this, SLOT(operator_clicked()));
connect(ui->pushButton_mul, SIGNAL(clicked(bool)), this, SLOT(operator_clicked()));
connect(ui->pushButton_div, SIGNAL(clicked(bool)), this, SLOT(operator_clicked()));
connect(ui->pushButton_result, SIGNAL(clicked(bool)), this, SLOT(equal_clicked()));
connect(ui->pushButton_clear, SIGNAL(clicked(bool)), this, SLOT(clear_clicked()));
}
mainwindow::~mainwindow()
{
delete ui;
}
void mainwindow::number_clicked()
{
// 数字按钮的槽
QPushButton *num = dynamic_cast<QPushButton *>(sender());
QString str = ui->lineEdit->text();
ui->lineEdit->setText(str+num->text());
}
void mainwindow::operator_clicked()
{
// 运算符按钮的槽
addFlag = false;
subFlag = false;
mulFlag = false;
divFlag = false;
QPushButton *op = dynamic_cast<QPushButton *>(sender());
QString str = ui->lineEdit->text();
ui->lineEdit->setText(str+op->text());
QString opVal = op->text();
if(QString::compare(opVal, "+", Qt::CaseInsensitive) == 0){
addFlag = true;
}else if(QString::compare(opVal, "-", Qt::CaseInsensitive) == 0){
subFlag = true;
}else if(QString::compare(opVal, "*", Qt::CaseInsensitive) == 0){
mulFlag = true;
}else if(QString::compare(opVal, "/", Qt::CaseInsensitive) == 0){
divFlag = true;
}
}
void mainwindow::equal_clicked()
{
// 等式按钮的槽
QPushButton *op = dynamic_cast<QPushButton *>(sender());
QString str = ui->lineEdit->text();
QList <QString> list;
if (addFlag)
{
list = str.split("+");
ans = list.at(0).toDouble() + list.at(1).toDouble();
}
else if(subFlag)
{
list = str.split("-");
ans = list.at(0).toDouble() - list.at(1).toDouble();
}
else if(mulFlag)
{
list = str.split("*");
ans = list.at(0).toDouble() * list.at(1).toDouble();
}
else if(divFlag)
{
list = str.split("/");
ans = list.at(0).toDouble() / list.at(1).toDouble();
}
QString StringAns = QString::number(ans);
ui->lineEdit->setText(str+op->text());
ui->lineEdit_2->setText(StringAns);
}
void mainwindow::clear_clicked()
{
// 清除按钮的槽
ui->lineEdit->setText("");
ui->lineEdit_2->setText("");
}
main.cpp的代码
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
mainwindow w;
w.show();
return a.exec();
}
3、程序效果(目前只能进行一次运算)