自定义 取值范围的控件
如下图所示
功能 : 输入取值范围
(
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() && strRight.isEmpty())
{
return false;
}
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;
}
}
if (left > right)
{
return false;
}
}
else
{
return false;
}
return true;
}
void WithinRangeEdit::setValue(double left , double right )
{
if (left > DBL_MIN)
{
m_lineEidtLeft->setText(QString::number(left, 10, 5));
}
if (right < DBL_MAX)
{
m_lineEidtRight->setText(QString::number(right, 10, 5));
}
}
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的输入:可以输入正负 整数或者浮点数