当前位置:网站首页>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
边栏推荐
- Proteus Software beginner notes
- 535. TinyURL 的加密与解密 : 设计一个 URL 简化系统
- Unexpected ‘debugger‘ statement no-debugger
- MySQL数据库主从同步,一致性解决方案
- MySQL master-slave synchronous asynchronous replication semi synchronous replication full synchronous replication
- ERP Kingdee for preparing BOM
- Engineering practice behind dall-e 2: ensure that the output of the model complies with the content policy
- Go Senior Engineer required course | I sincerely suggest you listen to it. Don't miss it~
- Gbase8s database select has order by Clause 3
- Recommended model reproduction (II): fine arrangement model deepfm, DIN
猜你喜欢

Murphy safety was selected for signing 24 key projects of Zhongguancun Science City

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

MySQL 主从复制原理以及流程

参加2022年杭州站Cocos Star Meetings

Proteus Software beginner notes

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

Artbench: the first class balanced, high-quality, clean annotated and standardized artwork generation data set

Kyligence Zen, an intelligent indicator driven management and decision-making platform, is newly launched and is in limited internal testing

Can software related inventions be patented in India?

倍福TwinCAT配置、调试第三方伺服详细讲解--以汇川IS620N为例子
随机推荐
How to create new user for ORACLE 19c (CDB & PDB)
模糊图片变清晰,一键双色图片,快速整理本地图片...这8个在线图片工具申请加入你的收藏夹!
Nacos startup error
NvtBack
Paper reproduction - ac-fpn:attention-guided context feature pyramid network for object detection
Factorization of large numbers ← C language
【LeetCode】14、最长公共前缀
Interpolated scatter data
Uncover the practice of Baidu intelligent test in the field of automatic test execution
File contained log poisoning (user agent)
智能指标驱动的管理和决策平台 Kyligence Zen 全新上线,限量内测中
推荐模型复现(二):精排模型DeepFM、DIN
Cmake error
Recommended model reproduction (II): fine arrangement model deepfm, DIN
535. encryption and decryption of tinyurl: design a URL simplification system
MySQL master-slave synchronous asynchronous replication semi synchronous replication full synchronous replication
Gbase8s database select has order by Clause 6
How to create new user for ORACLE 19c (CDB & PDB)
After class assignment of module 5 of the construction practice camp
地球观测卫星数据