当前位置:网站首页>QT addition calculator (simple case)
QT addition calculator (simple case)
2022-07-04 23:15:00 【Yinzz2】
Learn through a small case Qt
Realization function 1. Enter two numbers , Press “=” Button to display the calculation results
2. Both operands must be legal numbers , Refuse to accept any illegal characters
3. Both operands must be legal ,“=” The button is activated , Otherwise, disable ( You can't click )
4. The control that displays the results can only be viewed and cannot be modified , But it supports copying to the clipboard
5. The size and position of all sub windows are automatically adjusted to the best with the scaling of the main window
Code section :( Difficulties have notes )
test.h
#ifndef _TEST_H
#define _TEST_H
#include<QLabel>
#include<QLineEdit> // Row edit control
#include<QHBoxLayout> // Horizontal placer
#include<QDialog>
#include<QPushButton>
#include<QDoubleValidator> // Tester
class sum:public QDialog
{
Q_OBJECT
public:
sum(void);
public slots:
void enablebutton(void);
void get_result(void);
private:
QLineEdit* edit_x;
QLineEdit* edit_y;
QLineEdit* edit_z;
QLabel* m_label;
QPushButton* m_button;
};
#endiftest.cpp
#include<test.h>
sum::sum(void)
{
/*--------**** Initialize each parameter ****---------------*/
setWindowTitle(" Calculator ");
edit_x = new QLineEdit(this); //this That is, the current parent class pointer
edit_x->setAlignment(Qt::AlignLeft); // Align text left
edit_x->setValidator(new QDoubleValidator(this)); // Set up the digital verifier , Only numeric content can be entered
edit_y = new QLineEdit(this);
edit_y->setAlignment(Qt::AlignLeft);
edit_y->setValidator(new QDoubleValidator(this));
edit_z = new QLineEdit(this);
edit_z->setAlignment(Qt::AlignLeft); // Set read-only
edit_z->setReadOnly(true);
m_label = new QLabel("+",this);
m_button = new QPushButton("=",this);
m_button->setEnabled(false); // Initially set to disabled
/*--------**** Create a horizontal layout ****---------------*/
QHBoxLayout* layout = new QHBoxLayout(this);
layout->addWidget(edit_x);
layout->addWidget(m_label);
layout->addWidget(edit_y);
layout->addWidget(m_button);
layout->addWidget(edit_z);
setLayout(layout); // Set the layout
/*-------******** Slot function and signal ******----------------*/
connect(edit_x,SIGNAL(textChanged(QString)),this,SLOT(enablebutton(void)));
connect(edit_y,SIGNAL(textChanged(QString)),this,SLOT(enablebutton(void)));
connect(m_button,SIGNAL(clicked(void)),this,SLOT(get_result(void)));
}
//text() Get input text (QString) toDouble() take QString Convert to Double
void sum::enablebutton(void)
{
bool judge_x,judge_y;
edit_x->text().toDouble(&judge_x);
edit_y->text().toDouble(&judge_y);
m_button->setEnabled(judge_x && judge_y);
}
//number(): take double convert to QString
//setText(const QString &text)
void sum::get_result(void)
{
double res = edit_x->text().toDouble() + edit_y->text().toDouble();
QString str = QString::number(res);
edit_z->setText(str);
}
main.cpp
#include<test.h>
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
sum w;
w.show();
return a.exec();
}
Experimental phenomena :

边栏推荐
- 时间 (计算)总工具类 例子: 今年开始时间和今年结束时间等
- The Chinese output of servlet server and client is garbled
- Network namespace
- JS card style countdown days
- Google collab trample pit
- 小程序vant tab组件解决文字过多显示不全的问题
- 【ODX Studio編輯PDX】-0.2-如何對比Compare兩個PDX/ODX文件
- Docker镜像的缓存特性和Dockerfile
- S32 Design Studio for ARM 2.2 快速入门
- Insert sort, select sort, bubble sort
猜你喜欢

Set up a website with a sense of ceremony, and post it to 1/2 of the public network through the intranet

Explanation of bitwise operators

S32 Design Studio for ARM 2.2 快速入门

The difference between debug and release

MariaDB的Galera集群-双主双活安装设置
![P2181 对角线和P1030 [NOIP2001 普及组] 求先序排列](/img/79/36c46421bce08284838f68f11cda29.png)
P2181 对角线和P1030 [NOIP2001 普及组] 求先序排列

vim编辑器知识总结

Redis:Redis的事务

【图论】拓扑排序

Stm32 Reverse Introduction to CTF Competition Interpretation
随机推荐
Servlet+jdbc+mysql simple web exercise
How can enterprises cross the digital divide? In cloud native 2.0
CTF竞赛题解之stm32逆向入门
【js】-【动态规划】-笔记
MP进阶操作: 时间操作, sql,querywapper,lambdaQueryWapper(条件构造器)快速筛选 枚举类
【爬虫】数据提取之JSONpath
【ODX Studio编辑PDX】-0.2-如何对比Compare两个PDX/ODX文件
位运算符讲解
[graph theory] topological sorting
Excel shortcut keys - always add
HMS core machine learning service
[crawler] jsonpath for data extraction
Redis getting started complete tutorial: publish and subscribe
Compare two vis in LabVIEW
Ffmpeg quick clip
Excel 快捷键-随时补充
MariaDB's Galera cluster application scenario -- multi master and multi active databases
Google Earth engine (GEE) - globfire daily fire data set based on mcd64a1
Photoshop batch adds different numbers to different pictures
ScriptableObject