当前位置:网站首页>QT custom control: value range
QT custom control: value range
2022-06-29 12:48:00 【Mayfly wing*】
Customize Value range control
As shown in the figure below 
function : Input value range ( m i n , m a x ) (min,max) (min,max), Get the value range
WithinRangeEdit.h file
#pragma once
#include<QWidget>
#include<QLabel>
#include<QLineEdit>
#include<QHBoxLayout>
class WithinRangeEdit : public QWidget
{
Q_OBJECT
public:
WithinRangeEdit(QWidget *parent = nullptr);
~WithinRangeEdit();
// Get the value range entered by the user , If canBeDefaulted=true Allow default
bool getValue(double &left, double &right, bool canBeDefaulted=true);
// Clear all inputs
void clear();
private:
QLabel *m_labelLeft=nullptr ,*m_labelRight = nullptr, *m_labelCernter = nullptr;
QLineEdit *m_lineEidtLeft = nullptr,*m_lineEidtRight = nullptr;
QHBoxLayout *m_layout = nullptr;
// Initialize internal controls
void init();
};
Concrete realization
#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]+$))")));// Positive and negative integers and floating point numbers ( The maximum limit after the decimal point is 8 position )
m_lineEidtRight = new QLineEdit();
m_lineEidtRight->setValidator(new QRegExpValidator(QRegExp("^-?(([0-9]{0,16}(\\.[0-9]{1,8})$)|([0-9]+$))")));// Positive and negative integers and floating point numbers ( The maximum limit after the decimal point is 8 position ).
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) // Allow default : If the minimum value is empty , Then the minimum value is set to infinitesimal
{
left = DBL_MIN;
}
else
{
return false;
}
}
else
{
if (LeftOk)
{
return false;
}
}
if (strRight.isEmpty())
{
if (canBeDefaulted) // Allow default : If the maximum value is empty , Then the minimum value is set to infinity
{
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();
}
}
details : Control digital input :
m_lineEidtLeft->setValidator(new QRegExpValidator(QRegExp("^-?(([0-9]{0,16}(\\.[0-9]{1,8})$)|([0-9]+$))")));
Use regular expressions to limit lineEidt The input of : You can enter positive and negative Integer or floating point
边栏推荐
- [intelligent QBD risk assessment tool] Shanghai daoning brings you leanqbd introduction, trial and tutorial
- 中职网络安全技能竞赛之应用服务漏洞扫描与利用(SSH私钥泄露)
- Pangolin编译error: ‘numeric_limits’ is not a member of ‘std’
- Gbase8s database select has order by Clause 2
- 如何計算win/tai/loss in paired t-test
- 模糊图片变清晰,一键双色图片,快速整理本地图片...这8个在线图片工具申请加入你的收藏夹!
- 地球观测卫星数据
- Go Senior Engineer required course | I sincerely suggest you listen to it. Don't miss it~
- Mysql database master-slave synchronization, consistency solution
- Recurrence of recommended models (III): recall models youtubednn and DSSM
猜你喜欢

JVM之方法区
![[cloud native] 2.4 kubernetes core practice (middle)](/img/1e/b1b22caa03d499387e1a47a5f86f25.png)
[cloud native] 2.4 kubernetes core practice (middle)

Go Senior Engineer required course | I sincerely suggest you listen to it. Don't miss it~

Recommended model reproduction (II): fine arrangement model deepfm, DIN

Recurrence of recommended models (IV): multi task models esmm and MMOE

Recommended model recurrence (I): familiar with torch rechub framework and use

【JUC系列】同步工具类之ThreadLocal

An interpretable geometric depth learning model for structure based protein binding site prediction

oracle 19c : change the user sys/system username pasword under Linux

高校如何基于云原生构建面向未来的智慧校园?全栈云原生架构VS传统IT架构
随机推荐
Syntax of gbase8s database incompatible with for update clause
C#通过线索二叉树进行中序遍历输出
Understanding of P value
[环境配置]PWC-Net
Newton inequality
多项目开发入门-业务场景关联基础入门测试 工资表
oracle 19c : change the user sys/system username pasword under Linux
C#实现图的邻接矩阵和邻接表结构
在印度与软件相关的发明可不可以申请专利?
倍福控制器连接松下EtherCAT伺服注意事项
超 Nice 的表格响应式布局小技巧
Paper reproduction - ac-fpn:attention-guided context feature pyramid network for object detection
InDesign插件-常规功能开发-JS调试器打开和关闭-js脚本开发-ID插件
深入理解 volatile 关键字
[leetcode] 14. Longest public prefix
NvtBack
智能指标驱动的管理和决策平台 Kyligence Zen 全新上线,限量内测中
Murphy safety was selected for signing 24 key projects of Zhongguancun Science City
Gbase8s database into temp clause creates a temporary table to save query results.
【云原生】2.4 Kubernetes 核心实战(中)