Qt实现一个简单的计算器
作者:hackett
微信公众号:加班猿
一、UI界面版
运行效果:输入num1和num2选择+-*/点击计算即可
UI界面设计:
3个lineEdit(lineEditNum1,lineEditNum2,lineEditSum)
2个PushButton(buttonClear,ButtonCalc)
1个comboBox
1个label
布局:
3个lineEdit和1个comboBox、1个label水平布局
2个PushButton和一个弹簧水平布局
最后整体布局即可
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_buttonCalc_clicked()
{
double num1 = ui->lineEditNum1->text().toDouble();//获取num1的值
double num2 = ui->lineEditNum2->text().toDouble();//获取num2的值
uint8_t index = ui->comboBox->currentIndex(); //获取下拉框的下标0 +, 1 - ,2 *, 3 /
double result = 0.0;
switch (index) {
case 0:
result = num1 + num2;
break;
case 1:
result = num1 - num2;
break;
case 2:
result = num1 * num2;
break;
case 3:
if(num2 == 0)
{
QMessageBox::about(this,"注意","除数不能为0");
return;
}
result = num1 / num2;
break;
default:
break;
}
ui->lineEditSum->setText(QString::number(result));
}
void MainWindow::on_buttonClear_clicked()
{
ui->lineEditNum1->clear();
ui->lineEditNum2->clear();
ui->lineEditSum->clear();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_buttonCalc_clicked();//计算结果槽函数
void on_buttonClear_clicked();//清除输入框槽函数
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
二、纯代码版
注:只能两个数单独加减乘除,不能连续运算,按‘ = ’重新开始两个数运算
main.cpp
#include "calc.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Calc w;
w.show();
return a.exec();
}
calc.cpp
#include "calc.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QSizePolicy>
#include <QDebug>
Calc::Calc(QWidget *parent) :
QWidget(parent)
{
//获取字体QFont
QFont font = this->font();
font.setPixelSize(30);
this->setFont(font);
//保存键盘值
QString keys="789-456*123/0.+=";
QVBoxLayout *vbox = new QVBoxLayout();
QSizePolicy qsPolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
for(int i=0; i<4; i++)
{
QHBoxLayout *hbox = new QHBoxLayout();
for(int j=0; j<4; j++)
{
buttons[i*4+j] = new QToolButton(this);
//设置文本
buttons[i*4+j]->setText(keys.mid(i*4+j, 1));
hbox->addWidget(buttons[i*4+j]);
//策略设置
buttons[i*4+j]->setSizePolicy(qsPolicy);
//把按钮触发信号关联槽函数
connect(buttons[i*4+j], SIGNAL(clicked(bool)),this, SLOT(buttons_chick()));
}
//把水平布局管理器添加到垂直布局管理器中
vbox->addLayout(hbox);
}
//创建一个输入框
edit = new QLineEdit(this);
edit->setSizePolicy(qsPolicy);
edit->setMaximumHeight(60);
edit->setAlignment(Qt::AlignRight);
QVBoxLayout *allVbox = new QVBoxLayout(this);
allVbox->addWidget(edit);
allVbox->addLayout(vbox);
//初始化
firstNumber.clear();
secondNumber.clear();
calcflag = 0;
}
Calc::~Calc()
{
}
void Calc::input_data(QString str)
{
if(calcflag == 0)
{
firstNumber.append(str);
edit->setText(firstNumber);
}else
{
secondNumber.append(str);
edit->setText(secondNumber);
}
}
void Calc::buttons_chick()
{
//通过信号发送者获取按钮对象
QToolButton *button = (QToolButton*)sender();
//qDebug()
QString str = button->text();
QString datastr = ".0123456789";
QStringList typestr;
typestr<<"+"<<"-"<<"*"<<"/";
if(datastr.indexOf(str) >= 0)//获取按键值
{
input_data(str); //输入数据
}else if(typestr.indexOf(str)>=0)
{
calcflag = typestr.indexOf(str)+1;//获取运算符号type
}else //=
{
double result = 0.0;
switch (calcflag) {
case 1:
result = firstNumber.toDouble()+secondNumber.toDouble();
break;
case 2:
result = firstNumber.toDouble()-secondNumber.toDouble();
break;
case 3:
result = firstNumber.toDouble()*secondNumber.toDouble();
break;
case 4:
result = firstNumber.toDouble()/secondNumber.toDouble();
break;
default:
break;
}
edit->setText(QString::number(result));
calcflag = 0;
firstNumber.clear();
secondNumber.clear();
}
}
calc.h
#ifndef CALC_H
#define CALC_H
#include <QWidget>
#include <QToolButton>
#include <QLineEdit>
class Calc : public QWidget
{
Q_OBJECT
public:
explicit Calc(QWidget *parent = 0);
~Calc();
void input_data(QString str);
protected slots:
void buttons_chick();//按键槽函数
private:
QToolButton *buttons[16]; //按钮数组
QLineEdit *edit; //输入框
QString firstNumber; //记录输入第一个数值
QString secondNumber; //记录输入第一个数值
int calcflag; //1-add, 2-sub, 3-mul, 4-div
};
#endif // CALC_H
如果你觉得文章还不错,记得"点赞关注"
关注我的微信公众号【 加班猿 】可以获取更多内容
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。