目录
- 1 搭建标准界面
- 2、 逻辑编写
- 2.1 初始化
github链接:基于qt的计算器
更多内容可以点击这里查看个人博客:个人博客
1 搭建标准界面
按照下图搭设界面
修改样式让这计算器看起来更像一点,同时对按钮分组进行样式编辑,添加字符串name,为number,其他按键为other。之前的文章里出现过好几次不在赘述
修改最大的样式表
*
{
border:none;
background-color: rgb(243, 243, 243);
}
/* */
QPushButton
{
font: bold 12pt '微软雅黑';
}
QPushButton[name = "number"]
{
border-radius:4px;
background-color: rgb(252, 252, 252);
}
QPushButton[name = "other"]
{
border-radius:4px;
background-color: rgb(246, 246, 246);
}
/*增加鼠标悬浮背景色 */
QPushButton[name = "number"]:hover
{
border:1px solid rgb(193,193,193);
background-color: rgb(221, 223, 221);
}
QPushButton[name = "other"]:hover
{
border:1px solid rgb(193,193,193);
background-color: rgb(221, 223, 221);
}
接着修改文字输入部分,让他们文字全部右对齐,打开readonly,光标就不会闪烁
2、 逻辑编写
2.1 初始化
首先初始化获得所有的按钮,放入组,方便后续操作,在自己写个槽函数用于处理点击的流程
void cell_Standui::initUI()
{//1、把所有的按钮放到一个按钮组里边QButtonGroup * buttonGroup = new QButtonGroup(this);auto btnList = findChildren<QPushButton*>();for(auto btn : btnList){buttonGroup->addButton(btn);}//2、关联按钮组的点击信号connect(buttonGroup,&QButtonGroup::buttonClicked,this,&cell_Standui::onButtonGroupClicked);vec.resize(5);}
显示数字,记录比较简单,难点在于各种符号的切换,以及过程和最终结果的显示,在一开始输入时需要清除0,按完=号后,在次输入要清空记录;使用容器记录存着的数和操作过程;根号和平方等只有一个运算数,单独写;使用一个prebtn来记录上一个按键,若是一次计算完成则清空
#include "cell_standui.h"
#include "ui_cell_standui.h"cell_Standui::cell_Standui(QWidget *parent): QMainWindow(parent), ui(new Ui::cell_Standui)
{ui->setupUi(this);initUI();
}cell_Standui::~cell_Standui()
{delete ui;
}void cell_Standui::initUI()
{//1、把所有的按钮放到一个按钮组里边QButtonGroup * buttonGroup = new QButtonGroup(this);auto btnList = findChildren<QPushButton*>();for(auto btn : btnList){buttonGroup->addButton(btn);}//2、关联按钮组的点击信号connect(buttonGroup,&QButtonGroup::buttonClicked,this,&cell_Standui::onButtonGroupClicked);vec.resize(5);
}void cell_Standui::onButtonGroupClicked(QAbstractButton *btn)
{//获取按钮的名称// qDebug()<<btn->objectName();//首先把框里的数赋值auto val1 = ui->le_result->text().toFloat();QStringList nameList = btn->objectName().split('_');QString name = nameList[1];if(name >="0" && name <= "9" || name == "point" ){//把一开始的0清除if(name != "point" && ui->le_result->text() == "0"){ui->le_result->clear();}//如果点击数字键的时候输入框有数据,而且表达式框只有两个数据,则重置为刚刚输入的if(preBtn == "add" || preBtn == "multiplication" ||preBtn == "subtraction" || preBtn == "divide"){ui->le_result->clear();}else if(preBtn == "result" || preBtn == "squareroot" || preBtn == "square" || preBtn == "quarter"){//判断上次的按钮是不是=,如果是那么一次计算就算完成了,清空所有内容ui->le_result->clear();ui->le_history->clear();vec.clear();vec.resize(5);}//输入数字ui->le_result->insert(btn->text());}else if(name == "ce"){//清空ui->le_result->clear();}else if(name == "c"){//清空ui->le_result->clear();ui->le_history->clear();vec.clear();vec.resize(5);}else if(name == "del"){//删除//先让光标的位置移动到前一位ui->le_result->setCursorPosition(ui->le_result->cursorPosition()-1);ui->le_result->del();}//输入符号else if(name == "add"){if(vec.size() != 2){//把左操作数和运算符保存起来vec[0] = val1;vec[1] = "+";}}else if(name == "subtraction"){if(vec.size() != 2){//把左操作数和运算符保存起来vec[0] = val1;vec[1] = "-";}}else if(name == "multiplication"){if(vec.size() != 2){//把左操作数和运算符保存起来vec[0] = val1;vec[1] = "×";}}else if(name == "divide"){if(vec.size() != 2){//把左操作数和运算符保存起来vec[0] = val1;vec[1] = "÷";}}else if(name == "result"){vec[2] = val1;vec[3] = "=";if(vec[1] == "+"){vec[4] = vec[0].toFloat()+vec[2].toFloat();}else if(vec[1] == "-"){vec[4] = vec[0].toFloat()-vec[2].toFloat();}else if(vec[1] == "×"){vec[4] = vec[0].toFloat()*vec[2].toFloat();}else if(vec[1] == "÷"){vec[4] = vec[0].toFloat()/vec[2].toFloat();}ui->le_result->clear();ui->le_result->setText(vec[4].toString());}else if(name == "squareroot"){//把左操作数和运算符保存起来vec[0] = "√ ";vec[1] = val1;vec[2] = "=" ;vec[3] = sqrt(val1);ui->le_result->clear();ui->le_result->setText(vec[3].toString());}else if(name == "square"){//把左操作数和运算符保存起来vec[0] = val1;vec[1] = "²";vec[2] = "=" ;vec[3] = val1*val1;ui->le_result->clear();ui->le_result->setText(vec[3].toString());}else if(name == "quarter"){//把左操作数和运算符保存起来vec[0] = "1/";vec[1] = val1;vec[2] = "=" ;vec[3] = 1/val1;ui->le_result->clear();ui->le_result->setText(vec[3].toString());}else if(name == "mod"){//把左操作数和运算符保存起来vec[0] = val1;vec[1] = "%";vec[2] = "=" ;vec[3] = val1/100;ui->le_result->clear();ui->le_result->setText(vec[3].toString());}//显示表达式,每次都清空重新显示ui->le_history->clear();for(auto var : vec){ui->le_history->insert(var.toString());}preBtn = name;
}
头文件
#ifndef CELL_STANDUI_H
#define CELL_STANDUI_H#include <QMainWindow>
#include <QButtonGroup>>
#include <QDebug>
#include <QVector>
#include <QVariant>namespace Ui {
class cell_Standui;
}class cell_Standui : public QMainWindow
{Q_OBJECTpublic:explicit cell_Standui(QWidget *parent = nullptr);~cell_Standui();void initUI();public slots:void onButtonGroupClicked(QAbstractButton* btn);private:Ui::cell_Standui *ui;QVector<QVariant> vec;//记录上一个按钮的名字QString preBtn;
};#endif // CELL_STANDUI_H
成果图