当前位置:网站首页>qt 自定义控件 :取值范围
qt 自定义控件 :取值范围
2022-06-29 12:03:00 【蜉蝣之翼*】
自定义 取值范围的控件
如下图所示
功能 : 输入取值范围 ( m i n , m a x ) (min,max) (min,max),获取取值范围
WithinRangeEdit.h 文件
#pragma once
#include<QWidget>
#include<QLabel>
#include<QLineEdit>
#include<QHBoxLayout>
class WithinRangeEdit : public QWidget
{
Q_OBJECT
public:
WithinRangeEdit(QWidget *parent = nullptr);
~WithinRangeEdit();
//获取用户输入的取值范围 ,如果canBeDefaulted=true 则允许缺省
bool getValue(double &left, double &right, bool canBeDefaulted=true);
//清空所有输入
void clear();
private:
QLabel *m_labelLeft=nullptr ,*m_labelRight = nullptr, *m_labelCernter = nullptr;
QLineEdit *m_lineEidtLeft = nullptr,*m_lineEidtRight = nullptr;
QHBoxLayout *m_layout = nullptr;
//初始化内部控件
void init();
};
具体实现
#include "WithinRangeEdit.h"
#include<QRegExpValidator>
WithinRangeEdit::WithinRangeEdit(QWidget *parent ): QWidget(parent)
{
this->init();
}
WithinRangeEdit::~WithinRangeEdit()
{
}
void WithinRangeEdit::init()
{
QFont font("Microsoft YaHei", 14);
m_labelLeft = new QLabel(" (");
m_labelLeft->setFont(font);
m_labelRight = new QLabel(") ");
m_labelRight->setFont(font);
m_labelCernter = new QLabel(" - ");
m_labelCernter->setFont(font);
m_lineEidtLeft = new QLineEdit();
m_lineEidtLeft->setValidator(new QRegExpValidator(QRegExp("^-?(([0-9]{0,16}(\\.[0-9]{1,8})$)|([0-9]+$))")));//正负整数和浮点数(小数点后限制最多8位)
m_lineEidtRight = new QLineEdit();
m_lineEidtRight->setValidator(new QRegExpValidator(QRegExp("^-?(([0-9]{0,16}(\\.[0-9]{1,8})$)|([0-9]+$))")));//正负整数和浮点数(小数点后限制最多8位).
m_layout = new QHBoxLayout();
m_layout->addWidget(m_labelLeft,1, Qt::AlignHCenter);
m_layout->addWidget(m_lineEidtLeft, 1, Qt::AlignHCenter);
m_layout->addWidget(m_labelCernter, 1, Qt::AlignHCenter);
m_layout->addWidget(m_lineEidtRight, 1, Qt::AlignHCenter);
m_layout->addWidget(m_labelRight, 1, Qt::AlignHCenter);
m_layout->setContentsMargins(0, 0, 0, 0);
m_layout->setStretch(0,0);
m_layout->setStretch(1, 1);
m_layout->setStretch(2, 0);
m_layout->setStretch(3, 1);
m_layout->setStretch(4, 0);
this->setLayout(m_layout);
}
bool WithinRangeEdit::getValue(double &left, double &right, bool canBeDefaulted)
{
if (m_lineEidtLeft && m_lineEidtRight)
{
QString strLeft = m_lineEidtLeft->text();
bool LeftOk;
left = strLeft.toDouble(&LeftOk);
QString strRight = m_lineEidtRight->text();
bool RightOk;
right = strRight.toDouble(&RightOk);
if (strLeft.isEmpty())
{
if (canBeDefaulted) //允许缺省:如果最小值为空,则最小值设为无穷小
{
left = DBL_MIN;
}
else
{
return false;
}
}
else
{
if (LeftOk)
{
return false;
}
}
if (strRight.isEmpty())
{
if (canBeDefaulted) //允许缺省:如果最大值为空,则最小值设为无穷大
{
right = DBL_MAX;
}
else
{
return false;
}
}
else
{
if (RightOk)
{
return false;
}
}
}
else
{
return false;
}
}
void WithinRangeEdit::clear()
{
if (m_lineEidtLeft)
{
m_lineEidtLeft->clear();
}
if (m_lineEidtRight)
{
m_lineEidtRight->clear();
}
}
细节:控制数字的输入:
m_lineEidtLeft->setValidator(new QRegExpValidator(QRegExp("^-?(([0-9]{0,16}(\\.[0-9]{1,8})$)|([0-9]+$))")));
采用正则表达式限制lineEidt的输入:可以输入正负 整数或者浮点数
边栏推荐
- Cocos star meetings at Hangzhou station in 2022
- 在印度与软件相关的发明可不可以申请专利?
- MySQL 主从复制原理以及流程
- Gbase8s database select has order by Clause 6
- Gbase8s database into standard and into raw clauses
- Engineering practice behind dall-e 2: ensure that the output of the model complies with the content policy
- ERP preparation of BOM basis
- 1. Opencv实现简单颜色识别
- How to fix ORA-01017:用户名/口令无效 登录拒绝
- Uncover the practice of Baidu intelligent test in the field of automatic test execution
猜你喜欢

墨菲安全入选中关村科学城24个重点项目签约

Cache consistency, delete cache, write cache, cache breakdown, cache penetration, cache avalanche

Interview shock 61: tell me about MySQL transaction isolation level?

How to install oracle19c in Centos8

go 学习-搭建开发环境vscode开发环境golang

After class assignment of module 5 of the construction practice camp

架构实战营第五模块课后作业
![[intelligent QBD risk assessment tool] Shanghai daoning brings you leanqbd introduction, trial and tutorial](/img/00/9a6d17844b88f6921ad488f4975684.png)
[intelligent QBD risk assessment tool] Shanghai daoning brings you leanqbd introduction, trial and tutorial

多项目开发入门-业务场景关联基础入门测试 工资表

1. opencv realizes simple color recognition
随机推荐
Interview shock 61: tell me about MySQL transaction isolation level?
AES-128-CBC-Pkcs7Padding加密PHP实例
Gbase8s database select has order by Clause 6
oracle 19c : change the user sys/system username pasword under Linux
求大数的阶乘 ← C语言
对p值的理解
Blurred pictures become clear, one button two-color pictures, quickly organize local pictures These 8 online picture tools apply to join your favorites!
Codeforces Round #803 (Div. 2)
MySQL数据库主从同步,一致性解决方案
超 Nice 的表格响应式布局小技巧
【智能QbD风险评估工具】上海道宁为您带来LeanQbD介绍、试用、教程
Uncover the practice of Baidu intelligent test in the field of automatic test execution
Cocos star meetings at Hangzhou station in 2022
MIT线性代数中文笔记
huffman编码
Is it safe for Orient Fortune Securities to open an account? Handling of securities account opening
推荐模型复现(二):精排模型DeepFM、DIN
架构实战营第五模块课后作业
【综合案例】信用卡虚拟交易识别
模糊图片变清晰,一键双色图片,快速整理本地图片...这8个在线图片工具申请加入你的收藏夹!