当前位置:网站首页>QT basic hand training applet - simple calculator design (with source code, analysis)
QT basic hand training applet - simple calculator design (with source code, analysis)
2022-07-28 09:21:00 【Fly 】
use qt Designing a small program of calculator is very novice to practice , Here we will teach you how to realize the simplest four arithmetic calculators .
First step
1. First , I think we should figure out what functions the computer we want to have , General layout , This is a simple example of a small program , So I won't consider complex functions , There may be a lot of bug.
2. A simple calculator , Four operations should still be available ,+,-,*,/, Then another one equals , A clear
3. There should be 0-9 Ten figures , Add, subtract, multiply and divide , be equal to , Clear a total of... With a display text box at the end 17 A button
The second step
1. Now that we have figured out what controls we have , Let's first declare the controls we need in the future
QLineEdit *le_lcd; // Set a single line text input box QPushButton *bt_num[10]; // Set up 1,2,3...9,0; Ten key positions , A separate new Out 10 Press a button , It's a silly thing for us programmers , How simple how to , So we use pointer array to realize QPushButton *bt_add; // Set up a + French key QPushButton *bt_sub; // Set up a — French key QPushButton *bt_mul; // Set a multiplication key QPushButton *bt_div; // Set a division key QPushButton *bt_calc; // Set a confirmation equal to the key QPushButton *bt_chop; // Set the key to clear one character
The third step
1. Then we should give the declared control to new come out
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("=");
Step four
Control is ready , With declaration and space , Now let's lay out these controls
To help you understand the layout , I won't use only one layout
QHBoxLayout *hbox = new QHBoxLayout; // Horizontal layout keys hbox->addWidget(le_lcd); hbox->addWidget(bt_chop); QGridLayout *gbox = new QGridLayout; // Grid layout keys 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; // Vertical layout keys mainbox->addLayout(hbox); mainbox->addLayout(gbox); setLayout(mainbox); // This means , The whole is a vertical layout ,mainbox The front two layouts are installed inside
Step five
Set slot function declaration
public slots: void num_pressed(void); // Key slot function void del_num(void); // Delete an input void get_op(void); // Operator key slot void calculate(); // Calculation slot
Step six
Set the signals and slots associated with the front and rear stations
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()));
Step seven
Specific implementation of slot function
void Widget::del_num() // Delete an input { QString str = le_lcd->text(); str.chop(1); le_lcd->setText(str); } // Operator key slot void Widget::get_op() { //1. Extract key QPushButton *xbt = static_cast<QPushButton*>( sender() ); //2. Save symbols op = xbt->text().toStdString().c_str()[0]; //3. Extract the first operand data1 = le_lcd->text().toInt(); le_lcd->clear(); } void Widget::calculate() // Calculation slot { //0. extract data2 data2 = le_lcd->text().toInt(); //1. Calculation 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. Show le_lcd->setText(QString::number(answer)); } void Widget::num_pressed(void) // Operator key slot { //1. Extract key QPushButton *xbt = static_cast<QPushButton*>( sender() ); //2. Display the data le_lcd->setText(le_lcd->text().append(xbt->text())); }
Step eight
Go back to the header file declaration to define what we use for calculation
char op; int data1; int data2;
Step nine
Beautify it
//5. beautify (QSS) this->setStyleSheet("QPushButton{" "color:red;" "background-color:rgb(8,189,253);" "border-radius:3px;" "};");
Complete code
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>
#include <QLineEdit>
class Widget : public QWidget
{
Q_OBJECT
public slots:
void num_pressed(void); // Key slot function
void del_num(void); // Delete an input
void get_op(void); // Operator key slot
void calculate(); // Calculation slot
public:
Widget(QWidget *parent = 0);
~Widget();
//1. Declare the controls needed in the future
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;
int data2;
};
#endif // WIDGET_Hwidget.cpp
#include "widget.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
//2. All controls new come out
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("=");
//3. Layout
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);
//4. Front and back desk Association
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()));
//5. beautify (QSS)
// this->setStyleSheet("QPushButton{"
// "color:red;"
// "background-color:rgb(8,189,253);"
// "border-radius:3px;"
// "};");
}
void Widget::del_num()
{
QString str = le_lcd->text();
str.chop(1);
le_lcd->setText(str);
}
void Widget::get_op()
{
//1. Extract key
QPushButton *xbt = static_cast<QPushButton*>( sender() );
//2. Save symbols
op = xbt->text().toStdString().c_str()[0];
//3. Extract the first operand
data1 = le_lcd->text().toInt();
le_lcd->clear();
}
void Widget::calculate()
{
//0. extract data2
data2 = le_lcd->text().toInt();
//1. Calculation
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. Show
le_lcd->setText(QString::number(answer));
}
void Widget::num_pressed(void)
{
//1. Extract key
QPushButton *xbt = static_cast<QPushButton*>( sender() );
//2. Display the data
le_lcd->setText(le_lcd->text().append(xbt->text()));
}
Widget::~Widget()
{
}main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
design sketch :

边栏推荐
- 2022 safety officer-b certificate examination simulated 100 questions and answers
- 2022年安全员-B证考试模拟100题及答案
- MySQL 8.0.30 GA
- Sentinel
- Machine learning (11) -- time series analysis
- 2022年安全员-B证考试模拟100题及答案
- DN-DETR 论文精度,并解析其模型结构 & 2022年CVPR论文
- 01 tensorflow calculation model (I) - calculation diagram
- C#简单调用FMU ,进行仿真计算
- MQTT.js 入门教程:学习笔记
猜你喜欢
![[附下载]推荐几款暴力破解和字典生成的工具](/img/c6/f4a9c566ff21a8e133a8a991108201.png)
[附下载]推荐几款暴力破解和字典生成的工具
![[English postgraduate entrance examination vocabulary training camp] day 15 - analyze, general, avoid, surveillance, compared](/img/a8/2c2fab613035f5e50524056d5f51a3.png)
[English postgraduate entrance examination vocabulary training camp] day 15 - analyze, general, avoid, surveillance, compared

蓝牙技术|2025年北京充电桩总规模达70万个,聊聊蓝牙与充电桩的不解之缘

Get started quickly with flask (I) understand the framework flask, project structure and development environment

Title and answer of work permit for safety management personnel of hazardous chemical business units in 2022

Bluetooth technology | the total scale of charging piles in Beijing will reach 700000 in 2025. Talk about the indissoluble relationship between Bluetooth and charging piles

Machine learning (11) -- time series analysis

linux初始化mysql时报错 FATAL ERROR: Could not find my-default.cnf

Promise learning notes

从开发转测试:我从零开始,一干就是6年的自动化测试历程
随机推荐
训练一个自己的分类 | 【包教包会,数据都准备好了】
【592. 分数加减运算】
VR panoramic shooting helps promote the diversity of B & B
2022年安全员-B证考试模拟100题及答案
51单片机存储篇:EEPROM(I2C)
Openshift 4 - use verticalpodautoscaler to optimize application resource request and limit
2022 high voltage electrician examination simulated 100 questions and simulated examination
Map of China province > City > level > District > Town > village 5-level linkage download [2019 and 2021]
[English postgraduate entrance examination vocabulary training camp] day 15 - analyze, general, avoid, surveillance, compared
IP protocol of network layer
Bluetooth technology | the total scale of charging piles in Beijing will reach 700000 in 2025. Talk about the indissoluble relationship between Bluetooth and charging piles
KEGG通路的从属/注释信息如何获取
完善的交叉编译环境记录 peta 生成的shell 脚本
MDM data quality application description
个人博客小程序
技术分享| 快对讲综合调度系统
C simply call FMU for simulation calculation
mysql5.7.38容器里启动keepalived
剑指offer
Vs2015 use dumpbin to view the exported function symbols of the library