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

边栏推荐
- 【二叉树】节点与其祖先之间的最大差值
- Redis入门完整教程:哈希说明
- The solution to the lack of pcntl extension under MAMP, fatal error: call to undefined function pcntl_ signal()
- Servlet服务器端和客户端中文输出乱码问题
- 时间 (计算)总工具类 例子: 今年开始时间和今年结束时间等
- Redis introduction complete tutorial: Collection details
- Pict generate orthogonal test cases tutorial
- Redis:Redis消息的发布与订阅(了解)
- MariaDB's Galera cluster application scenario -- multi master and multi active databases
- Google collab trample pit
猜你喜欢

Editplus-- usage -- shortcut key / configuration / background color / font size

Redis入门完整教程:列表讲解

【二叉树】节点与其祖先之间的最大差值

Redis démarrer le tutoriel complet: Pipeline

Complete tutorial for getting started with redis: bitmaps

Redis入门完整教程:Pipeline

C语言快速解决反转链表

Compare two vis in LabVIEW

The initial arrangement of particles in SPH (solved by two pictures)

Qt个人学习总结
随机推荐
MariaDB的Galera集群应用场景--数据库多主多活
Header file duplicate definition problem solving "c1014 error“
Actual combat simulation │ JWT login authentication
Redis入门完整教程:列表讲解
A complete tutorial for getting started with redis: hyperloglog
法国学者:最优传输理论下对抗攻击可解释性探讨
[Jianzhi offer] 6-10 questions
Advantages of Alibaba cloud international CDN
Redis入门完整教程:事务与Lua
[try to hack] wide byte injection
Application of machine learning in housing price prediction
List related knowledge points to be sorted out
[machine learning] handwritten digit recognition
HMS core unified scanning service
图片懒加载的原理
ScriptableObject
Redis入门完整教程:集合详解
MySQL数据库备份与恢复--mysqldump命令
The caching feature of docker image and dockerfile
刷题指南-public