1、嵌入式QT实验报告简单计算器的设计电 子 科 技 大 学学 生 实 验 报 告 书课 程 名 称 实用嵌入式应用系统设计与实现 任 课 老 师 实 验 名 称 简单功能计算器的设计 学 生 姓 名 学 生 学 号 学 生 学 院 2015 2016 学年 第 1 学期实验项目名称简单功能计算器的设计 实验日期2015.11.23一、实验内容 1、设计一个简单计算器并能完成简单的四则运算,实时更新运算结果; 2、带界面计算器自带数字及功能按钮,能实现更多以及更灵活的的计算功能。一、实验步骤设计(包括实验方案设计、实验步骤或算法描述等)实验设计方案如下: 图1 简单计算器的设计方案流程 图2 带显
2、示界面计算器的设计方案流程实验步骤:一、简单计算器的设计:1、创建一个Qt GUI应用项目,项目名称为dialog_designer ,基类选择Qwidget。项目建好后会自动生成一系列文件,双击界面文件widget.ui 进入Qt Designer 进行界面设计。2、界面设计完成后修改控件的各属性,并修改Tab顺序,点击工具栏上的的“编辑Tab顺序”按钮,依次按顺序点击即可,完成后再按工具栏上的编辑控件按钮返回。3、在头文件widget.h的Widget类添加共有槽函数;在源文件widget.cpp中添加新代码。4、用模拟器调试运行。二、带界面的计算器的设计: 与简单计算器的设计步骤基本相同
3、。但是需要在项目文件(后缀为.pro)添加一段代码,使得程序中能使用Qt脚本,在原有代码的基础上添加。在头文件widget.h的Widget类添加共有槽函数和全局变量,以及包含头文件QtScript,以使用Qt Script API。在源文件widget.cpp中添加新代码,之后即可运行调试。三、实验结果分析及代码(包括结果描述、综合分析及实现关键代码)实验结果如下:1、简单计算器:代码:(1)在widget.h文件中#ifndef WIDGET_H#define WIDGET_H#include namespace Ui class Widget;class Widget : public
4、QWidget Q_OBJECTpublic: explicit Widget(QWidget *parent = 0); Widget();private slots: void on_doubleSpinBox_1_valueChanged(); void on_doubleSpinBox_2_valueChanged(); void on_doubleSpinBox_3_valueChanged(); void on_doubleSpinBox_4_valueChanged(); void on_doubleSpinBox_5_valueChanged(); void on_double
5、SpinBox_6_valueChanged(); void on_doubleSpinBox_7_valueChanged(); void on_doubleSpinBox_8_valueChanged();private: Ui:Widget *ui;#endif / WIDGET_H(2)在widget.cpp源文件中#include widget.h#include ui_widget.hWidget:Widget(QWidget *parent) : QWidget(parent), ui(new Ui:Widget) ui-setupUi(this);void Widget:on_
6、doubleSpinBox_1_valueChanged() ui-outout_plus-setText(QString:number(ui-doubleSpinBox_1-value() + ui-doubleSpinBox_2-value();void Widget:on_doubleSpinBox_2_valueChanged() ui-outout_plus-setText(QString:number(ui-doubleSpinBox_1-value() + ui-doubleSpinBox_2-value();void Widget:on_doubleSpinBox_3_valu
7、eChanged() ui-outout_minus-setText(QString:number(ui-doubleSpinBox_3-value() - ui-doubleSpinBox_4-value(); void Widget:on_doubleSpinBox_4_valueChanged()ui-outout_minus-setText(QString:number(ui-doubleSpinBox_3-value() - ui-doubleSpinBox_4-value(); void Widget:on_doubleSpinBox_5_valueChanged() ui-out
8、out_multi-setText(QString:number(ui-doubleSpinBox_5-value() * ui-doubleSpinBox_6-value(); void Widget:on_doubleSpinBox_6_valueChanged() ui-outout_multi-setText(QString:number(ui-doubleSpinBox_5-value() * ui-doubleSpinBox_6-value(); void Widget:on_doubleSpinBox_7_valueChanged() ui-outout_div-setText(
9、QString:number(ui-doubleSpinBox_7-value() / ui-doubleSpinBox_8-value(); void Widget:on_doubleSpinBox_8_valueChanged() ui-outout_div-setText(QString:number(ui-doubleSpinBox_7-value() / ui-doubleSpinBox_8-value(); Widget:Widget() delete ui;2、带显示界面的计算器:代码:(1)在shiyanB.pro项目文件中QT += core guiQT += scriptg
10、reaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = shiyanBTEMPLATE = appSOURCES += main.cpp widget.cppHEADERS += widget.hFORMS += widget.ui(2)在头文件widget.h中#ifndef WIDGET_H#define WIDGET_H#include #include QWidgetnamespace Ui class Widget;class Widget : public QWidget Q_OBJECTpublic: QString sum;
11、/全局变量。 explicit Widget(QWidget *parent = 0); Widget();public slots:void clickNum1(); /按钮1的槽函数。void clickNum2(); /按钮2的槽函数。void clickNum3(); /按钮3的槽函数。void clickNum4(); /按钮4的槽函数。void clickNum5(); /按钮5的槽函数。void clickNum6(); /按钮6的槽函数。void clickNum7(); /按钮7的槽函数。void clickNum8(); /按钮8的槽函数。void clickNum9();
12、 /按钮9的槽函数。void clickNum0(); /按钮0的槽函数。void clickPoint(); /按钮.的槽函数。void clickLbracket(); /按钮(的槽函数。void clickRbracket(); /按钮)的槽函数。void clickClean(); /按钮clean的槽函数。void clickDelete(); /按钮delete的槽函数。void clickPlus(); /按钮+的槽函数。void clickMinus(); /按钮-的槽函数。void clickMulti(); /按钮*的槽函数。void clickDiv(); /按钮/的槽函
13、数。void clickEqul(); /按钮=的槽函数。private: Ui:Widget *ui;#endif / WIDGET_H(3)在源文件widget.cpp中#include widget.h#include ui_widget.hWidget:Widget(QWidget *parent) : QWidget(parent), ui(new Ui:Widget) ui-setupUi(this); sum=; connect(ui-pushButton_1,SIGNAL(clicked(),this,SLOT(clickNum1(); connect(ui-pushButto
14、n_2,SIGNAL(clicked(),this,SLOT(clickNum2(); connect(ui-pushButton_3,SIGNAL(clicked(),this,SLOT(clickNum3(); connect(ui-pushButton_4,SIGNAL(clicked(),this,SLOT(clickNum4(); connect(ui-pushButton_5,SIGNAL(clicked(),this,SLOT(clickNum5(); connect(ui-pushButton_6,SIGNAL(clicked(),this,SLOT(clickNum6();
15、connect(ui-pushButton_7,SIGNAL(clicked(),this,SLOT(clickNum7(); connect(ui-pushButton_8,SIGNAL(clicked(),this,SLOT(clickNum8(); connect(ui-pushButton_9,SIGNAL(clicked(),this,SLOT(clickNum9(); connect(ui-pushButton_0,SIGNAL(clicked(),this,SLOT(clickNum0(); connect(ui-pushButton_point,SIGNAL(clicked()
16、,this,SLOT(clickPoint(); connect(ui-pushButton_lbracket,SIGNAL(clicked(),this,SLOT(clickLbracket(); connect(ui-pushButton_rbracket,SIGNAL(clicked(),this,SLOT(clickRbracket(); connect(ui-pushButton_clean,SIGNAL(clicked(),this,SLOT(clickClean(); connect(ui-pushButton_delete,SIGNAL(clicked(),this,SLOT(
17、clickDelete(); connect(ui-pushButton_plus,SIGNAL(clicked(),this,SLOT(clickPlus(); connect(ui-pushButton_minus,SIGNAL(clicked(),this,SLOT(clickMinus(); connect(ui-pushButton_multi,SIGNAL(clicked(),this,SLOT(clickMulti(); connect(ui-pushButton_div,SIGNAL(clicked(),this,SLOT(clickDiv(); connect(ui-push
18、Button_equl,SIGNAL(clicked(),this,SLOT(clickEqul();void Widget:clickNum1() /按钮1的槽函数。QString num1=1;QString str=ui-lineEdit-text();str=str+num1;if(ui-lineEdit-text()=0)ui-lineEdit-setText(num1);elseui-lineEdit-setText(str);void Widget:clickNum2() /按钮2的槽函数。QString num1=2;QString str=ui-lineEdit-text()
19、;str=str+num1;if(ui-lineEdit-text()=0)ui-lineEdit-setText(num1);elseui-lineEdit-setText(str);void Widget:clickNum3() /按钮3的槽函数。QString num1=3;QString str=ui-lineEdit-text();str=str+num1;if(ui-lineEdit-text()=0)ui-lineEdit-setText(num1);elseui-lineEdit-setText(str);void Widget:clickNum4() /按钮4的槽函数。QSt
20、ring num1=4;QString str=ui-lineEdit-text();str=str+num1;if(ui-lineEdit-text()=0)ui-lineEdit-setText(num1);elseui-lineEdit-setText(str);void Widget:clickNum5() /按钮5的槽函数。QString num1=5;QString str=ui-lineEdit-text();str=str+num1;if(ui-lineEdit-text()=0)ui-lineEdit-setText(num1);elseui-lineEdit-setText
21、(str);void Widget:clickNum6() /按钮6的槽函数。QString num1=6;QString str=ui-lineEdit-text();str=str+num1;if(ui-lineEdit-text()=0)ui-lineEdit-setText(num1);elseui-lineEdit-setText(str);void Widget:clickNum7() /按钮7的槽函数。QString num1=7;QString str=ui-lineEdit-text();str=str+num1;if(ui-lineEdit-text()=0)ui-line
22、Edit-setText(num1);elseui-lineEdit-setText(str);void Widget:clickNum8() /按钮8的槽函数。QString num1=8;QString str=ui-lineEdit-text();str=str+num1;if(ui-lineEdit-text()=0)ui-lineEdit-setText(num1);elseui-lineEdit-setText(str);void Widget:clickNum9() /按钮9的槽函数。QString num1=9;QString str=ui-lineEdit-text();st
23、r=str+num1;if(ui-lineEdit-text()=0)ui-lineEdit-setText(num1);elseui-lineEdit-setText(str);void Widget:clickNum0() /按钮0的槽函数。QString num1=0;QString str=ui-lineEdit-text();str=str+num1;if(ui-lineEdit-text()=0)ui-lineEdit-setText(num1);elseui-lineEdit-setText(str);void Widget:clickPoint() /按钮point的槽函数。Q
24、String num1=.;QString num2=0.;QString str=ui-lineEdit-text();str=str+num1;if(ui-lineEdit-text()=0)ui-lineEdit-setText(num2);elseui-lineEdit-setText(str);void Widget:clickLbracket() /按钮lbracket的槽函数。QString num1=(;QString str=ui-lineEdit-text();str=str+num1;if(ui-lineEdit-text()=0)ui-lineEdit-setText(
25、num1);elseui-lineEdit-setText(str);void Widget:clickRbracket() /按钮rbracket的槽函数。QString num1=);QString str=ui-lineEdit-text();str=str+num1;if(ui-lineEdit-text()=0)ui-lineEdit-setText(num1);elseui-lineEdit-setText(str);void Widget:clickClean() /按钮clean的槽函数。QString num1=0;ui-lineEdit-setText(num1);void
26、 Widget:clickDelete() /按钮delete的槽函数。QString a=0;QString str=ui-lineEdit-text();if(str.length()=1)ui-lineEdit-setText(a);else if(str.length()1)str=str.left(str.length()-1); /从左边取长度减去1的字符串。ui-lineEdit-setText(str);void Widget:clickPlus() /按钮+的槽函数。QString num1=+;QString str=ui-lineEdit-text();sum=str+n
27、um1;ui-lineEdit-setText(sum);void Widget:clickMinus() /按钮-的槽函数。QString num1=-;QString str=ui-lineEdit-text();sum=str+num1;ui-lineEdit-setText(sum);void Widget:clickMulti() /按钮*的槽函数。QString num1=*;QString str=ui-lineEdit-text();sum=str+num1;ui-lineEdit-setText(sum);void Widget:clickDiv() /按钮/的槽函数。QSt
28、ring num1=/;QString str=ui-lineEdit-text();sum=str+num1;ui-lineEdit-setText(sum);void Widget:clickEqul() /按钮=的槽函数。QString str=ui-lineEdit-text();QScriptEngine engine; /QScriptEngine类为程序提供一个嵌入式脚本环境,每一个引擎都是一个轻量级自包含的虚拟机。QScriptValue result = engine.evaluate(str); /调用脚本引擎的evaluate()函数可以执行脚本,此处即可算出之前输入的字符串算式的结果。ui-lineEdit-setText(result.toString(); /调用QScriptValue类的toString()函数将QScriptValue类的执行结果转换成string型。Widget:Widget() delete ui;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1