当前位置:网站首页>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 :

边栏推荐
- Feign调用异常[Running, pool size = 10, active threads = 10, queued tasks = 0, completed tasks = n]
- VR panoramic shooting helps promote the diversity of B & B
- Sentry log management system installation and use tutorial
- 【多线程】long和double的非原子性协定
- C simply call FMU for simulation calculation
- 2022安全员-C证特种作业证考试题库及答案
- DAPP safety summary and typical safety incident analysis
- 使用 GBase C API 执行存储过程是怎样的?
- Machine learning (11) -- time series analysis
- VR全景拍摄,助力民宿多元化宣传
猜你喜欢

【单细胞高级绘图】07.KEGG富集结果展示

2022年安全员-B证考试模拟100题及答案

快速上手Flask(一) 认识框架Flask、项目结构、开发环境

Alibaba cloud server setup and pagoda panel connection

2022安全员-C证特种作业证考试题库及答案

DAPP safety summary and typical safety incident analysis

Learn to draw with nature communications -- complex violin drawing

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

Review the past and know the new MySQL isolation level

Dapp安全总结与典型安全事件分析
随机推荐
golang升级到1.18.4版本 遇到的问题
2022年安全员-B证考试模拟100题及答案
正则表达式为十六进制数字?
shell 实现harbor v1/v2的备份/恢复/迁移等功能
Code management platform SVN deployment practice
Solution and implementation of APP accelerating reading and displaying IPFs pictures
Promise学习笔记
Get started quickly with flask (I) understand the framework flask, project structure and development environment
5 运算符、表达式和语句
中国地图省>市>级>区>镇>村5级联动下载【2019和2021】
OpenShift 4 之AMQ Streams(1) - 多个Consumer从Partition接收数据
F - Jealous Two-二维逆序对
01-TensorFlow计算模型(一)——计算图
完善的交叉编译环境记录 peta 生成的shell 脚本
2022年危险化学品经营单位安全管理人员上岗证题目及答案
负数的十六进制表示
7 C control statements: branches and jumps
1.5 merge\rebase\revert\stash\branch
Larkapi access credentials overview
Recommend an artifact to get rid of the entanglement of variable names and a method to modify file names in batches