电子工程师技术服务社区
公告
登录
|
注册
首页
技术问答
厂商活动
正点原子
板卡试用
资源库
下载
文章
社区首页
文章
【野火i.MX6ULL ARM Linux开发板连载】轻小型计算器开发(一)
分 享
扫描二维码分享
【野火i.MX6ULL ARM Linux开发板连载】轻小型计算器开发(一)
野火
i.MX6ULL
ARM
瑟寒凌风
关注
发布时间: 2021-03-10
丨
阅读: 1723
对于嵌入式开发的我们所使用Qt,在原始 Qt 的基础上,做了许多出色的调整以适合嵌入式环境。删除掉了一些与嵌入式无关的Lib库,使其相对来说更加节省嵌入式平台的内存空间。 使用交叉编译器编译好的Qt库要在LINUX虚拟机和嵌入式开发板上各一份,有了这个库,我们就可以在上面尽情的编写Qt程序,而这个库在前面我们搭建qt开发环境的时候已经搭建完成。 在一个复杂的界面中,如下来的项目,一个基本的计算器,它有0~9十个数字按钮,四个运算按钮,还有删除和求值按钮等等。这些按钮整齐的排列在一起,我们可以自己用鼠标拖动或计算它们的位置然后设置他们的坐标,虽然麻烦了一点,但也能完成。不过,如果还想让按钮的大小随着窗口的改变而改变呢?从这些繁琐的事情中脱离出来专注于界面本身,布局管理器便诞生了。 # 设计基本计算器界面 我们平常使用的计算器,首先,有十个数字键,其次,有符号键和小数点,还有四个运算符号,再加上一个求值、归零、删除键。之后最上方一个显示条来显示结果。一个基本的计算器差不多就是这个样子了吧。下面我们用Qt Designer来设计一个计算器的界面。 首先我们先建立一个Qt窗口应用项目,之后再双击widget.ui转到设计模式,先向窗口内拖入十九个按钮,将这些按钮上的文字修改为如下图所示。 ![](https://cf03.ickimg.com/bbsimages/202103/4c333d149335c9cf57f6a99cc713bad9.jpg) 然后我们还需要一个显示条用来显示计算结果,用QLineEdit即可,如下图所示,按钮的摆放不整齐。 ![](https://cf03.ickimg.com/bbsimages/202103/7190e54e4389b6b67d31e256e1303372.jpg) 运行Qt Creator,依次点击Qt Creator菜单“文件”、“新建文件或项目(N)…”,在新建项目中选择“Application”、“Qt Widgets Application”。点击按钮“Choose…”后,输入项目名称calculator,选择你合适的路径。 ![](https://cf03.ickimg.com/bbsimages/202103/26580eb7aa69de6eb78b446c4c5dc908.jpg) 选中主窗口,将windowTitle改为Calculator。 ![](https://cf03.ickimg.com/bbsimages/202103/9743ac4d88323cf73cdcc8fbe7f09f5a.jpg) 在widget.h里面添加几个私有成员和私有函数 ```c private: bool calculate(double operand, QString pendingOperator); //终止运算,清除数据,报错 void abortOperation(); //连接信号和槽 void connectSlots(); //储存运算符 QString pendingOperator; //储存运算结果 double result; //标记是否等待一个操作数 bool waitForOperand; ``` ## 在widget.cpp里面添加函数的实现 先在widget.cpp的开头添加头文件#include
,再添加函数 ```c bool Widget::calculate(double operand, QString pendingOperator) { if(pendingOperator == "+") { result += operand; } else if(pendingOperator == "-") { result -= operand; } else if(pendingOperator == "*") { result *= operand; } else if(pendingOperator == "/") { if(operand == 0.0) return false; result /= operand; } return true; } void Widget::abortOperation() { result = 0.0; pendingOperator.clear(); ui->lineEdit->setText("0"); waitForOperand = true; QMessageBox::warning(this, "运算错误", "除数不能为零"); } ``` 在widget.cpp里添加槽函数实现。 ```c void Widget::digitClicked() { QPushButton *digitBtn = static_cast
(sender()); QString value = digitBtn->text(); if(ui->lineEdit->text() == "0" && value == "0") return; if(waitForOperand) { ui->lineEdit->setText(value); waitForOperand = false; } else { ui->lineEdit->setText(ui->lineEdit->text() + value); } } void Widget::on_clearBtn_clicked() { //将当前显示的数归零 ui->lineEdit->setText("0"); waitForOperand = true; } ``` ```c void Widget::on_clearAllBtn_clicked() { //将当前显示的数据归零,并将之前保存的数据运算清除 ui->lineEdit->setText("0"); waitForOperand = true; result = 0.0; pendingOperator.clear(); } void Widget::on_equalBtn_clicked() { double operand = ui->lineEdit->text().toDouble(); if(pendingOperator.isEmpty()) return; if(!calculate(operand, pendingOperator)) { abortOperation(); return; } ui->lineEdit->setText(QString::number(result)); pendingOperator.clear(); result = 0.0; waitForOperand = true; } void Widget::on_signBtn_clicked() { QString text = ui->lineEdit->text(); double value = text.toDouble(); if(value > 0) { text.prepend('-'); } else if(value < 0) { text.remove(0, 1); } ui->lineEdit->setText(text); } void Widget::operatorClicked() { QPushButton *clickedBtn = qobject_cast
(sender()); QString clickedOperator = clickedBtn->text(); double operand = ui->lineEdit->text().toDouble(); if(!pendingOperator.isEmpty()) { if(!calculate(operand, pendingOperator)) { abortOperation(); return; } ui->lineEdit->setText(QString::number(result)); } else { result = operand; } pendingOperator = clickedOperator; waitForOperand = true; } void Widget::on_pointBtn_clicked() { if (waitForOperand) ui->lineEdit->setText("0"); if (!ui->lineEdit->text().contains('.')) ui->lineEdit->setText(ui->lineEdit->text() + "."); waitForOperand = false; } ``` 在Widget类的构造函数里添加代码如下 ```c Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); ui->lineEdit->setText("0"); result = 0.0; waitForOperand = true; connectSlots(); } ``` ![](https://cf03.ickimg.com/bbsimages/202103/cf08eccb96d94e9302a9939e6ddd8a97.jpg)
原创作品,未经权利人授权禁止转载。详情见
转载须知
。
举报文章
点赞
(
1
)
瑟寒凌风
关注
评论
(0)
登录后可评论,请
登录
或
注册
相关文章推荐
MK-米客方德推出工业级存储卡
Beetle ESP32 C3 蓝牙数据收发
Beetle ESP32 C3 wifi联网获取实时天气信息
开箱测评Beetle ESP32-C3 (RISC-V芯片)模块
正点原子数控电源DP100测评
DP100试用评测-----开箱+初体验
Beetle ESP32 C3环境搭建
【花雕体验】16 使用Beetle ESP32 C3控制8X32位WS2812硬屏之二
X
你的打赏是对原创作者最大的认可
请选择打赏IC币的数量,一经提交无法退回 !
100IC币
500IC币
1000IC币
自定义
IC币
确定
X
提交成功 ! 谢谢您的支持
返回
我要举报该内容理由
×
广告及垃圾信息
抄袭或未经授权
其它举报理由
请输入您举报的理由(50字以内)
取消
提交