当前位置:网站首页>Qt加法计算器(简单案例)
Qt加法计算器(简单案例)
2022-07-04 22:34:00 【Yinzz2】
通过一个小案例学习Qt
实现功能1.输入两个数字,按“=”按钮显示计算结果
2.两个操作数必须都是合法的数字,拒绝接受任何非法字符
3.两个操作数必须全部合法,“=”按钮才被激活,否则禁用(不可以点击)
4.显示结果的控件只可查看不可修改,但支持复制到剪贴板
5.所有子窗口的大小和位置随主窗口的缩放自动调整至最佳
代码部分:(难点有注释)
test.h
#ifndef _TEST_H
#define _TEST_H
#include<QLabel>
#include<QLineEdit> //行编辑控件
#include<QHBoxLayout> //水平布局器
#include<QDialog>
#include<QPushButton>
#include<QDoubleValidator> //检验器
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)
{
/*--------****初始化各个参数****---------------*/
setWindowTitle("计算器");
edit_x = new QLineEdit(this); //this即为当前父类指针
edit_x->setAlignment(Qt::AlignLeft); //文本左对齐
edit_x->setValidator(new QDoubleValidator(this)); //设置数字验证器,只能输入数字形式内容
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); //设置只读
edit_z->setReadOnly(true);
m_label = new QLabel("+",this);
m_button = new QPushButton("=",this);
m_button->setEnabled(false); //一开始设置为禁用
/*--------****创建水平方向的布局器****---------------*/
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); //设置布局器
/*-------********槽函数与信号******----------------*/
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()获取输入文本(QString) toDouble()将QString转换为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():将double转换成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();
}
实验现象:
边栏推荐
- 通过Go语言创建CA与签发证书
- [OpenGL] note 29 anti aliasing (MSAA)
- Is Huatai Securities a nationally recognized securities firm? Is it safe to open an account?
- Analysis of the self increasing and self decreasing of C language function parameters
- 攻防世界 MISC 进阶区 3-11
- Duplicate ADMAS part name
- Wechat official account solves the cache problem of entering from the customized menu
- The small program vant tab component solves the problem of too much text and incomplete display
- 图片懒加载的原理
- Complete tutorial for getting started with redis: bitmaps
猜你喜欢
Editplus-- usage -- shortcut key / configuration / background color / font size
P2181 对角线和P1030 [NOIP2001 普及组] 求先序排列
Sobel filter
[roommate learned to use Bi report data processing in the time of King glory in one game]
Analysis of the self increasing and self decreasing of C language function parameters
Redis入门完整教程:集合详解
A complete tutorial for getting started with redis: Pipeline
Redis入門完整教程:Pipeline
位运算符讲解
Attack and defense world misc master advanced zone 001 normal_ png
随机推荐
Analysis of the self increasing and self decreasing of C language function parameters
MYSQL架构——逻辑架构
Editplus-- usage -- shortcut key / configuration / background color / font size
Photoshop batch adds different numbers to different pictures
EditPlus--用法--快捷键/配置/背景色/字体大小
Sword finger offer 65 Add without adding, subtracting, multiplying, dividing
[ODX studio edit PDX] - 0.2-how to compare two pdx/odx files of compare
Unity修仙手游 | lua动态滑动功能(3种源码具体实现)
剑指Offer 68 - II. 二叉树的最近公共祖先
剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
The difference between Max and greatest in SQL
mamp下缺少pcntl扩展的解决办法,Fatal error: Call to undefined function pcntl_signal()
Redis introduction complete tutorial: List explanation
Redis入门完整教程:API的理解和使用
集群的概述与定义,一看就会
Three stage operations in the attack and defense drill of the blue team
【室友用一局王者荣耀的时间学会了用BI报表数据处理】
新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码
Summary of index operations in mongodb
【剑指Offer】6-10题