当前位置:网站首页>QT专题1:实现一个简易计算器
QT专题1:实现一个简易计算器
2022-07-28 02:08:00 【迷途君】
QT程序开发流程如下:
1.申明必要的部件。
2.将部件构造出来
3必要的布局
4.前后台进行关联
5.美化(QSS)
QT基础部件:
1.按钮类:普通按钮,工具按钮,单选按钮,多选按钮,命令连接按钮
2.布局类:水平布局,垂直布局,网格布局
3.输出类:标签,文本浏览器,日历,七段数码管,进度条
4.输入类
5.容器
下面通过用QT实现一个简易计算器来初步了解QT的使用:
首先我们要先考虑计算器的需要使用到那些部件,0~9的数字按钮键,还有加减乘除运算符的按键,以及清除键,和一个文本框来显示我们输入以及计算的结果。
那么首先我们需要在头文件中去声明这些按钮!!这里我们用到了两个类,分别是QLineEdit和QPushButton
//1. 申明出未来需要的控件
QLineEdit *le_lcd;//显示框
QPushButton *bt_num[10];//数字键
QPushButton *bt_add;//+号键
QPushButton *bt_sub;//-
QPushButton *bt_mul;//*
QPushButton *bt_div;// /
QPushButton *bt_calc;// =
QPushButton *bt_chop;//清除在我们输入两个数以及运算符后,我们需要将两个数字和运算符进行保存,这样方便我们进行计算所以我们还需要类头文件中定义三个 变量来分别保存我们的数字和运算符
char op;//保存运算符
int data1;//保存计算数1
int data2;//保存计算数2随后我们需要去我们的cpp文件中,将这些部件构造出来!
le_lcd = new QLineEdit;
bt_chop = new QPushButton("<");
for(int i=0; i<10; i++)
bt_num[i] = new QPushButton(QString::number(i));
bt_add = new QPushButton("+");
bt_sub = new QPushButton("-");
bt_mul = new QPushButton("*");
bt_div = new QPushButton("/");
bt_calc = new QPushButton("=");随后我们要对这些按钮进行布局排版,还记得我们的的三种布局吗,水平布局QHBoxLayout,垂直布局QVBoxLayout,网格布局 QGridLayout,当然这几种布局我们可以混合使用!!
//3. 布局
QHBoxLayout *hbox = new QHBoxLayout;
hbox->addWidget(le_lcd);
hbox->addWidget(bt_chop);
QGridLayout *gbox = new QGridLayout;
int i = 0;
for(int y=0; y<3; y++)
for(int x=0; x<3; x++)
gbox->addWidget(bt_num[i++], y, x);
gbox->addWidget(bt_num[9], 3, 0);
gbox->addWidget(bt_add, 0, 3);
gbox->addWidget(bt_sub, 1, 3);
gbox->addWidget(bt_mul, 2, 3);
gbox->addWidget(bt_div, 3, 1);
gbox->addWidget(bt_calc, 3, 2, 1, 2);
QVBoxLayout *mainbox = new QVBoxLayout;
mainbox->addLayout(hbox);
mainbox->addLayout(gbox);
setLayout(mainbox);布局过后,我们需要将我们的按钮和功能相关联起来,这时候我们需要使用槽函数来进行前后台的关联,那么我们先在头文件中声明我们要创建的槽函数!
public slots:
void num_pressed(void); //按键槽函数
void del_num(void); //删除一个输入
void get_op(void); //运算符按键槽
void calculate(); //计算槽将按钮和槽函数进行关联:
//4. 前后台关联
for(int i=0; i<10; i++)
connect(bt_num[i], SIGNAL(clicked(bool)), this, SLOT(num_pressed()));
connect(bt_chop, SIGNAL(clicked(bool)), this, SLOT(del_num()));
connect(bt_add, SIGNAL(clicked(bool)), this, SLOT(get_op()));
connect(bt_sub, SIGNAL(clicked(bool)), this, SLOT(get_op()));
connect(bt_mul, SIGNAL(clicked(bool)), this, SLOT(get_op()));
connect(bt_div, SIGNAL(clicked(bool)), this, SLOT(get_op()));
connect(bt_calc, SIGNAL(clicked(bool)), this, SLOT(calculate()));随后我们在cpp文件中对槽函数的功能进行编写
void Widget::del_num()
{
QString str = le_lcd->text();
str.chop(1);
le_lcd->setText(str);
}
void Widget::get_op()
{
//1. 提取按键
QPushButton *xbt = static_cast<QPushButton*>( sender() );
//2. 保存符号
op = xbt->text().toStdString().c_str()[0];
//3. 提取第一个运算数
data1 = le_lcd->text().toInt();
le_lcd->clear();
}
void Widget::calculate()
{
//0. 提取data2
data2 = le_lcd->text().toInt();
//1. 计算
int answer;
switch (op) {
case '+':
answer = data1 + data2;
break;
case '-':
answer = data1 - data2;
break;
case '*':
answer = data1 * data2;
break;
case '/':
answer = data1 / data2;
break;
default:
break;
}
//2. 显示
le_lcd->setText(QString::number(answer));
}
void Widget::num_pressed(void)
{
//1. 提取按键
QPushButton *xbt = static_cast<QPushButton*>( sender() );
//2. 显示数据
le_lcd->setText(le_lcd->text().append(xbt->text()));
}那么这样一个简易的计算器就完成了!!!
边栏推荐
- GBase8s如何在有外键关系的表中删除数据
- Building of APP automation environment (I)
- CSDN TOP1“一个处女座的程序猿“如何通过写作成为百万粉丝博主?
- Vscode debug displays multiple columns of data
- P6118 [joi 2019 final] solution to the problem of Zhenzhou City
- 数据中台夯实数据基础
- JS event object 2 e.charcode character code e.keycode key code box moves up, down, left and right
- Data center construction (III): introduction to data center architecture
- [brother hero's July training] day 27: picture
- 使用PyTorch的TensorBoard-可视化深度学习指标 | PyTorch系列(二十五)
猜你喜欢

trivy【1】工具扫描运用

Flutter神操作学习之(满级攻略)

Introduction to the reduce() function in JS

vscode debug显示多列数据

Pycharm 快速给整页全部相同名称修改的快捷键

Data Lake: flume, a massive log collection engine
[email protected]注解使用"/>[email protected]注解使用

Development and design logic of rtsp/onvif protocol easynvr video platform one click upgrade scheme

Center Based 3D object detection and tracking (centerpoint) paper notes

智能工业设计软件公司天洑C轮数亿元融资
随机推荐
Skills in writing English IEEE papers
JS event object offsetx/y clientx y pagex y
Trivy [1] tool scanning application
[signal processing] weak signal detection in communication system based on the characteristics of high-order statistics with matlab code
mysql 随笔
pytest最好的测试框架
Docker advanced -redis cluster configuration in docker container
智能工业设计软件公司天洑C轮数亿元融资
Redis AOF log persistence
Typescript (zero) -- introduction, environment construction, first instance
Constant power wireless charging based on stm32
Where do I go to open an account for stock speculation? Is it safe to open an account on my mobile phone
MySQL essay
Oracle basicfile lob field space recycling shrink space doubts
[email protected]注解使用
数据中台夯实数据基础
Unexpected harvest of epic distributed resources, from basic to advanced are full of dry goods, big guys are strong!
Commissioning experience of ROS
There is no way to predict the rise and fall of tomorrow
[red team] att & CK - file hiding