嵌入式Qt 实现用户界面与业务逻辑分离

news/发布时间2024/5/15 17:56:49

一.基本程序框架一般包含

二.框架的基本设计原则

三.用户界面与业务逻辑的交互

 

 四.代码实现计算器用户界面与业务逻辑

ICalculator.h

#ifndef _ICALCULATOR_H_
#define _ICALCULATOR_H_#include <QString>class ICalculator
{
public:virtual bool expression(const QString& exp) = 0;virtual QString result() = 0;
};#endif

QCalculator.h

#ifndef _QCALCULATOR_H_
#define _QCALCULATOR_H_#include "QCalculatorUI.h"
#include "QCalculatorDec.h"class QCalculator
{
protected:QCalculatorUI* m_ui;QCalculatorDec m_cal;QCalculator();bool construct();
public:static QCalculator* NewInstance();void show();~QCalculator();
};#endif // QCALCULATOR_H

QCalculator.cpp

#include "QCalculator.h"QCalculator::QCalculator()
{
}bool QCalculator::construct()
{m_ui = QCalculatorUI::NewInstance();if( m_ui != NULL ){m_ui->setCalculator(&m_cal);}return (m_ui != NULL);
}QCalculator* QCalculator::NewInstance()
{QCalculator* ret = new QCalculator();if( (ret == NULL) || !ret->construct() ){delete ret;ret = NULL;}return ret;
}void QCalculator::show()
{m_ui->show();
}QCalculator::~QCalculator()
{delete m_ui;
}

QCalculatorDec.h

#ifndef _CALCULATORCORE_H_
#define _CALCULATORCORE_H_#include <QString>
#include <QStack>
#include <QQueue>#include "ICalculator.h"class QCalculatorDec : public ICalculator
{
protected:QString m_exp;QString m_result;bool isDigitOrDot(QChar c);bool isSymbol(QChar c);bool isSign(QChar c);bool isNumber(QString s);bool isOperator(QString s);bool isLeft(QString s);bool isRight(QString s);int priority(QString s);bool match(QQueue<QString>& exp);QString calculate(QQueue<QString>& exp);QString calculate(QString l, QString op, QString r);bool transform(QQueue<QString>& exp, QQueue<QString>& output);QQueue<QString> split(const QString& exp);
public:QCalculatorDec();~QCalculatorDec();bool expression(const QString& exp);QString expression();QString result();
};#endif

 QCalculatorDec.cpp

#include "QCalculatorDec.h"QCalculatorDec::QCalculatorDec()
{m_exp = "";m_result = "";
}QCalculatorDec::~QCalculatorDec()
{}bool QCalculatorDec::isDigitOrDot(QChar c)
{return (('0' <= c) && (c <= '9')) || (c == '.');
}bool QCalculatorDec::isSymbol(QChar c)
{return isOperator(c) || (c == '(') || (c == ')');
}bool QCalculatorDec::isSign(QChar c)
{return (c == '+') || (c == '-');
}bool QCalculatorDec::isNumber(QString s)
{bool ret = false;s.toDouble(&ret);return ret;
}bool QCalculatorDec::isOperator(QString s)
{return (s == "+") || (s == "-") || (s == "*") || (s == "/");
}bool QCalculatorDec::isLeft(QString s)
{return (s == "(");
}bool QCalculatorDec::isRight(QString s)
{return (s == ")");
}int QCalculatorDec::priority(QString s)
{int ret = 0;if( (s == "+") || (s == "-") ){ret = 1;}if( (s == "*") || (s == "/") ){ret = 2;}return ret;
}bool QCalculatorDec::expression(const QString& exp)
{bool ret = false;QQueue<QString> spExp = split(exp);QQueue<QString> postExp;m_exp = exp;if( transform(spExp, postExp) ){m_result = calculate(postExp);ret = (m_result != "Error");}else{m_result = "Error";}return ret;
}QString QCalculatorDec::result()
{return m_result;
}QQueue<QString> QCalculatorDec::split(const QString& exp)
{QQueue<QString> ret;QString num = "";QString pre = "";for(int i=0; i<exp.length(); i++){if( isDigitOrDot(exp[i]) ){num += exp[i];pre = exp[i];}else if( isSymbol(exp[i]) ){if( !num.isEmpty() ){ret.enqueue(num);num.clear();}if( isSign(exp[i]) && ((pre == "") || (pre == "(") || isOperator(pre)) ){num += exp[i];}else{ret.enqueue(exp[i]);}pre = exp[i];}}if( !num.isEmpty() ){ret.enqueue(num);}return ret;
}bool QCalculatorDec::match(QQueue<QString>& exp)
{bool ret = true;int len = exp.length();QStack<QString> stack;for(int i=0; i<len; i++){if( isLeft(exp[i]) ){stack.push(exp[i]);}else if( isRight(exp[i]) ){if( !stack.isEmpty() && isLeft(stack.top()) ){stack.pop();}else{ret = false;break;}}}return ret && stack.isEmpty();
}bool QCalculatorDec::transform(QQueue<QString>& exp, QQueue<QString>& output)
{bool ret = match(exp);QStack<QString> stack;output.clear();while( ret && !exp.isEmpty() ){QString e = exp.dequeue();if( isNumber(e) ){output.enqueue(e);}else if( isOperator(e) ){while( !stack.isEmpty() && (priority(e) <= priority(stack.top())) ){output.enqueue(stack.pop());}stack.push(e);}else if( isLeft(e) ){stack.push(e);}else if( isRight(e) ){while( !stack.isEmpty() && !isLeft(stack.top()) ){output.enqueue(stack.pop());}if( !stack.isEmpty() ){stack.pop();}}else{ret = false;}}while( !stack.isEmpty() ){output.enqueue(stack.pop());}if( !ret ){output.clear();}return ret;
}QString QCalculatorDec::calculate(QString l, QString op, QString r)
{QString ret = "Error";if( isNumber(l) && isNumber(r) ){double lp = l.toDouble();double rp = r.toDouble();if( op == "+" ){ret.sprintf("%f", lp + rp);}else if( op == "-" ){ret.sprintf("%f", lp - rp);}else if( op == "*" ){ret.sprintf("%f", lp * rp);}else if( op == "/" ){const double P = 0.000000000000001;if( (-P < rp) && (rp < P) ){ret = "Error";}else{ret.sprintf("%f", lp / rp);}}else{ret = "Error";}}return ret;
}QString QCalculatorDec::calculate(QQueue<QString>& exp)
{QString ret = "Error";QStack<QString> stack;while( !exp.isEmpty() ){QString e = exp.dequeue();if( isNumber(e) ){stack.push(e);}else if( isOperator(e) ){QString rp = !stack.isEmpty() ? stack.pop() : "";QString lp = !stack.isEmpty() ? stack.pop() : "";QString result = calculate(lp, e, rp);if( result != "Error" ){stack.push(result);}else{break;}}else{break;}}if( exp.isEmpty() && (stack.size() == 1) && isNumber(stack.top()) ){ret = stack.pop();}return ret;
}

QCalculatorUI.h

#ifndef _QCALCULATORUI_H_
#define _QCALCULATORUI_H_#include <QWidget>
#include <QLineEdit>
#include <QPushButton>#include "ICalculator.h"class QCalculatorUI : public QWidget
{Q_OBJECT
private:QLineEdit* m_edit;QPushButton* m_buttons[20];ICalculator* m_cal;QCalculatorUI();bool construct();
private slots:void onButtonClicked();
public:static QCalculatorUI* NewInstance();void show();void setCalculator(ICalculator* cal);ICalculator* getCalculator();~QCalculatorUI();
};#endif

QCalculatorUI.cpp

#include "QCalculatorUI.h"
#include <QDebug>QCalculatorUI::QCalculatorUI() : QWidget(NULL, Qt::WindowCloseButtonHint)
{m_cal = NULL;
}bool QCalculatorUI::construct()
{bool ret = true;const char* btnText[20] ={"7", "8", "9", "+", "(","4", "5", "6", "-", ")","1", "2", "3", "*", "<-","0", ".", "=", "/", "C",};m_edit = new QLineEdit(this);if( m_edit != NULL ){m_edit->move(10, 10);m_edit->resize(240, 30);m_edit->setReadOnly(true);m_edit->setAlignment(Qt::AlignRight);}else{ret = false;}for(int i=0; (i<4) && ret; i++){for(int j=0; (j<5) && ret; j++){m_buttons[i*5 + j] = new QPushButton(this);if( m_buttons[i*5 + j] != NULL ){m_buttons[i*5 + j]->resize(40, 40);m_buttons[i*5 + j]->move(10 + (10 + 40)*j, 50 + (10 + 40)*i);m_buttons[i*5 + j]->setText(btnText[i*5 + j]);connect(m_buttons[i*5 + j], SIGNAL(clicked()), this, SLOT(onButtonClicked()));}else{ret = false;}}}return ret;
}QCalculatorUI* QCalculatorUI::NewInstance()
{QCalculatorUI* ret = new QCalculatorUI();if( (ret == NULL) || !ret->construct() ){delete ret;ret = NULL;}return ret;
}void QCalculatorUI::show()
{QWidget::show();setFixedSize(width(), height());
}void QCalculatorUI::onButtonClicked()
{QPushButton* btn = dynamic_cast<QPushButton*>(sender());if( btn != NULL ){QString clickText = btn->text();if( clickText == "<-" ){QString text = m_edit->text();if( text.length() > 0 ){text.remove(text.length()-1, 1);m_edit->setText(text);}}else if( clickText == "C" ){m_edit->setText("");}else if( clickText == "=" ){if( m_cal != NULL ){m_cal->expression(m_edit->text());m_edit->setText(m_cal->result());}}else{m_edit->setText(m_edit->text() + clickText);}}
}void QCalculatorUI::setCalculator(ICalculator* cal)
{m_cal = cal;
}ICalculator* QCalculatorUI::getCalculator()
{return m_cal;
}QCalculatorUI::~QCalculatorUI()
{}

main.cpp

#include <QApplication>#include "QCalculator.h"int main(int argc, char *argv[])
{QApplication a(argc, argv);QCalculator* cal = QCalculator::NewInstance();int ret = -1;if( cal != NULL ){cal->show();ret = a.exec();delete cal;}return ret;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.bcls.cn/bAyZ/5529.shtml

如若内容造成侵权/违法违规/事实不符,请联系编程老四网进行投诉反馈email:xxxxxxxx@qq.com,一经查实,立即删除!

相关文章

嵌入式中逻辑分析仪基本操作方法

前期准备 1.一块能触摸的屏对应的主板机 2.逻辑分析仪对应的软件工具 3.对应的拓展板 4.确定拓展板的引脚分布情况 第一步&#xff1a;逻辑分析仪j基本操作 1.数据捕捉需要先进行对应软件安装,并按照需求进行配置 2.这里以A20为例:此手机使用显示驱动芯片CST148,触摸屏分辨…

Python爬虫实战:从API获取数据

引言 在现代软件开发中&#xff0c;API已经成为获取数据的主要方式之一。API允许不同的软件应用程序相互通信&#xff0c;共享数据和功能。在本文中&#xff0c;我们将学习如何使用Python从API获取数据&#xff0c;并探讨其在实际应用中的价值。 目录 引言 二、API基础知识 …

【c语言】字符函数和字符串函数(下)

前言 书接上回 【c语言】字符函数和字符串函数(上) 上一篇讲解的strcpy、strcat、strcmp函数的字符串长度是不受限制的 而本篇strncpy、strncat、strcnmp函数的字符串长度是受限制的 欢迎关注个人主页&#xff1a;逸狼 创造不易&#xff0c;可以点点赞吗~ 如有错误&#xff0c;…

2024年Apache DolphinScheduler RoadMap:引领开源调度系统的未来

非常欢迎大家来到Apache DolphinScheduler社区&#xff01;随着开源技术在全球范围内的快速发展&#xff0c;社区的贡献者 “同仁” 一直致力于构建一个强大而活跃的开源调度系统社区&#xff0c;为用户提供高效、可靠的任务调度和工作流管理解决方案。 在过去的一段时间里&…

Facebook的数字社交使命:连接世界的下一步

在数字化时代&#xff0c;社交媒体已成为人们生活的重要组成部分&#xff0c;而Facebook作为其中最具影响力的平台之一&#xff0c;一直以来都在努力履行着自己的使命——连接世界。然而&#xff0c;随着时代的变迁和技术的发展&#xff0c;Facebook正在不断探索着连接世界的下…

如何使用GAP-Burp-Extension扫描潜在的参数和节点

关于GAP-Burp-Extension GAP-Burp-Extension是一款功能强大的Burp扩展&#xff0c;该工具在getAllParams扩展的基础上进行了升级&#xff0c;该工具不仅可以帮助广大研究人员在安全审计过程中扫描潜在的参数&#xff0c;而且还可以搜索潜在的链接并使用这些参数进行测试&#…

论文阅读:《High-Resolution Image Synthesis with Latent Diffusion Models》

High-Resolution Image Synthesis with Latent Diffusion Models 论文链接 代码链接 What’s the problem addressed in the paper?(这篇文章究竟讲了什么问题&#xff1f;比方说一个算法&#xff0c;它的 input 和 output 是什么&#xff1f;问题的条件是什么) 这篇文章提…

热点参数流控(Sentinel)

热点参数流控 热点流控 资源必须使用注解 SentinelResource 编写接口 以及 热点参数流控处理器 /*** 热点流控 必须使用注解 SentinelResource* param id* return*/ RequestMapping("/getById/{id}") SentinelResource(value "getById", blockHandler …

【Android移动开发】Windows10平台安装Android Studio与人工智能算法模型部署案例

目录 一、Android Studio下载地址二、开发环境JDK三、开始安装Android Studio四、案例展示与搭建五、人工智能算法模型移动端部署案例参考 一、Android Studio下载地址 https://developer.android.google.cn/studio/install.html 电脑配置要求&#xff1a; 下载保存在指定文…

【算法与数据结构】463、LeetCode岛屿的周长

文章目录 一、题目二、解法三、完整代码 所有的LeetCode题解索引&#xff0c;可以看这篇文章——【算法和数据结构】LeetCode题解。 一、题目 二、解法 思路分析&#xff1a;直接利用公式法&#xff0c;遇到一对相邻的陆地&#xff0c;总周长就减去2。那么周长公式为&#xff1…

【数据分享】2012-2022年全国及各城市POI(兴趣点)数据(超20G)

#1资源介绍 POI(一般作为Point of Interest的缩写,也有Point of Information的说法),通常称作兴趣点,泛指互联网电子地图中的点类数据,基本包含名称、地址、坐标、类别四个属性;随着互联网电子地图服务与LBS应用的普及,POI无论从概念范畴,还是从信息纵深都有了长足发展…

SQL Server ID 自增不连续、删除数据后再次插入ID不连续

背景 当我们使用SQL Server 进行数据库操作时&#xff0c;经常会把 Table 的 ID 设置成主键自增 PRIMARY KEY IDENTITY&#xff0c;但是这样做存在一个问题就是 当我们删除一行数据后&#xff0c;再次添加后会看到ID的顺序不连续&#xff0c;如下所示。 查询一下&#xff1a;…

springboot-基础-eclipse配置+helloword示例

备份笔记。所有代码都是2019年测试通过的&#xff0c;如有问题请自行搜索解决&#xff01; 目录 配置helloword示例新建项目创建文件 配置 spring boot官方有定制版eclipse&#xff0c;也就是STS&#xff0c;因为不想再装&#xff0c;所以考虑eclipse插件安装jdk和eclipse安装…

接口测试 —— Jmeter读取数据库数据作测试参数

1、添加Jdbc Request 2、添加ForEach控制器(右键线程组->逻辑控制器->ForEach控制器) ①输入变量的前缀&#xff1a;mobilephone&#xff1b; 从jdbc request设置的变量得知&#xff0c;我们要取的值为mobilephone_1、mobilephone_2、mobilephone_3......所以这里输入m…

MySQL之大表删除(基于硬链接方式)

在DROP TABLE的时候&#xff0c;所有进程不管是DDL还是DML都被HANG起&#xff1b;直到DROP结束才继续执行&#xff1b;这是因为INNODB会维护一个全局独占锁&#xff08;在table cache上面&#xff09;&#xff0c;直到DROP TABLE完成才释放。在我们常用的ext3,ext4&#xff0c;…

JVM跨代引用垃圾回收

1. 跨代引用概述 在Java堆内存中&#xff0c;年轻代和老年代之间存在的对象相互引用&#xff0c;假设现在要进行一次新生代的YGC&#xff0c;但新生代中的对象可能被老年代所引用的&#xff0c;为了找到新生代中的存活对象&#xff0c;不得不遍历整个老年代。这样明显效率很低…

java高级——反射

目录 反射概述反射的使用获取class对象的三种方式反射获取类的构造器1. 获取类中所有的构造器2. 获取单个构造器 反射获取构造器的作用反射获取成员变量反射变量赋值、取值获取类的成员方法反射对象类方法执行 反射简易框架案例案例需求实现步骤代码如下 反射概述 什么是反射 反…

较通用web脚手架模板搭建

较通用web脚手架模板搭建 从这里开始就接触到以后写项目的思维了。 做一个web开发&#xff0c;那就要层次分明&#xff0c;要有个实现的规划&#xff0c;这通常也是有一个较为通用的模板的。 总的来说&#xff1a;不同的层次有不同的模块&#xff0c;每个模块有必须实现的功…

Jessibuca 插件播放直播流视频

jessibuca官网&#xff1a;http://jessibuca.monibuca.com/player.html git地址&#xff1a;https://gitee.com/huangz2350_admin/jessibuca#https://gitee.com/link?targethttp%3A%2F%2Fjessibuca.monibuca.com%2F 项目需要的文件 1.播放组件 <template ><div i…

【算法 - 动态规划】找零钱问题Ⅰ

在前面的动态规划系列文章中&#xff0c;关于如何对递归进行分析的四种基本模型都介绍完了&#xff0c;再来回顾一下&#xff1a; 从左到右模型 &#xff1a;arr[index ...] 从 index 之前的不用考虑&#xff0c;只考虑后面的该如何选择 。范围尝试模型 &#xff1a;思考 [L ,…
推荐文章