当前位置:网站首页>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;
};
#endif
test.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 :
边栏推荐
- Google collab trample pit
- [crawler] XPath for data extraction
- QT drawing network topology diagram (connecting database, recursive function, infinite drawing, dragging nodes)
- Qualcomm WLAN framework learning (30) -- components supporting dual sta
- Redis入门完整教程:有序集合详解
- 推荐收藏:跨云数据仓库(data warehouse)环境搭建,这货特别干!
- Stm32 Reverse Introduction to CTF Competition Interpretation
- Servlet+JDBC+MySQL简单web练习
- Object detection based on OpenCV haarcascades
- 微信公众号解决从自定义菜单进入的缓存问题
猜你喜欢
Docker镜像的缓存特性和Dockerfile
Redis introduction complete tutorial: Collection details
Redis démarrer le tutoriel complet: Pipeline
Redis: redis message publishing and subscription (understand)
小程序vant tab组件解决文字过多显示不全的问题
壁仞科技研究院前沿技术文章精选
Google Earth engine (GEE) - globfire daily fire data set based on mcd64a1
Redis入门完整教程:哈希说明
phpcms付费阅读功能支付宝支付
智力考验看成语猜古诗句微信小程序源码
随机推荐
OSEK标准ISO_17356汇总介绍
Tweenmax emoticon button JS special effect
ETCD数据库源码分析——处理Entry记录简要流程
该如何去选择证券公司,手机上开户安不安全
ECS settings SSH key login
Actual combat simulation │ JWT login authentication
Redis入门完整教程:事务与Lua
[crawler] XPath for data extraction
Servlet服务器端和客户端中文输出乱码问题
Notepad++--编辑的技巧
[machine learning] handwritten digit recognition
[crawler] jsonpath for data extraction
Object detection based on OpenCV haarcascades
Question brushing guide public
HMS core machine learning service
Redis入门完整教程:集合详解
PS style JS webpage graffiti board plug-in
PICT 生成正交测试用例教程
Basic knowledge of database
Redis introduction complete tutorial: detailed explanation of ordered collection