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

边栏推荐
- MySQL 8.0.30 GA
- App加速读取显示IPFS图片的解决方案和实现
- 【592. 分数加减运算】
- Send a message to the background when closing the page
- How to obtain the subordinate / annotation information of KEGG channel
- 技术分享| 快对讲综合调度系统
- Get started quickly with flask (I) understand the framework flask, project structure and development environment
- Alibaba cloud server setup and pagoda panel connection
- VR全景拍摄,助力民宿多元化宣传
- C#简单调用FMU ,进行仿真计算
猜你喜欢

一年涨薪三次背后的秘密

IDC脚本文件运行
![[advanced drawing of single cell] 07. Display of KEGG enrichment results](/img/77/0a9006743734c1993785d087f957f0.png)
[advanced drawing of single cell] 07. Display of KEGG enrichment results

IP protocol of network layer

12 common design ideas of design for failure

正负数值的正则表达式

RGB-T追踪——【多模态融合】Visible-Thermal UAV Tracking: A Large-Scale Benchmark and New Baseline

final关键字和枚举类型

Promise learning notes

Linux initializes MySQL with fatal error: could not find my-default.cnf
随机推荐
Linux initializes MySQL with fatal error: could not find my-default.cnf
Learn to draw with nature communications -- complex violin drawing
478-82(56、128、718、129)
Vs2015 use dumpbin to view the exported function symbols of the library
【杂谈】程序员的发展最需要两点能力
IntelliJ IDEA 关联数据库
10. Learn MySQL like clause
C language array pointer and pointer array discrimination, analysis of memory leakage
Feign调用异常[Running, pool size = 10, active threads = 10, queued tasks = 0, completed tasks = n]
网络层的IP协议
Oracle-11gR2默认的系统JOB
Data analysis interview question summary
力扣题(1)—— 两数之和
Principle of line of sight tracking and explanation of the paper
js数组去重,id相同对某值相加合并
【单细胞高级绘图】07.KEGG富集结果展示
Sword finger offer
Introduction to official account
MDM data quality application description
Implementation principle of golang synergy