QLineEdit限制经纬度输入范围和整形输入范围,double和int采用两种不同的方式进行。
int输入范围限制,重写QIntValidator
class MyIntValidator : public QIntValidator
{
Q_OBJECT
public:
explicit MyIntValidator(QObject * parent = 0) :QIntValidator(parent)
{
}
MyIntValidator(int bottom, int top, QObject * parent) :QIntValidator(bottom, top, parent)
{
}
virtual void setRange(int bottom, int top)
{
QIntValidator::setRange(bottom, top);
}
~MyIntValidator()
{
}
virtual State validate(QString &s, int &n) const
{
return QIntValidator::validate(s, n);
}
virtual void fixup(QString &s) const
{
//s = QString("%1").arg(bottom());
s = QString("");
}
};
double输入限制
class MyDoubleValidator : public QDoubleValidator
{
Q_OBJECT
public:
explicit MyDoubleValidator(QObject * parent = 0) :QDoubleValidator(parent)
{
}
MyDoubleValidator(double bottom, double top,int decimals, QObject * parent) :QDoubleValidator(bottom, top, decimals, parent)
{
}
virtual void setRange(double bottom, double top, int decimals)
{
QDoubleValidator::setRange(bottom, top, decimals);
}
~MyDoubleValidator()
{
}
virtual State validate(QString &str, int &n) const
{
if (())
{
return QValidator::Intermediate;
}
int a = 1;
bool cOK = false;
double val = (&cOK);
if (!cOK)
{
return QValidator::Invalid;
}
int dotPos = (".");
if (dotPos > 0)
{
if ((() - dotPos - 1).length() > decimals())
{
return QValidator::Invalid;
}
}
if (val< top() && val > bottom())
{
return QValidator::Acceptable;
}
return QValidator::Invalid;
}
virtual void fixup(QString &s) const
{
//s = QString("%1").arg(bottom());
s = QString("");
}
};
使用方法:
->setValidator(new MyDoubleValidator(-180.0, 180.0,8, ));//8代表小数位数
->setValidator(new MyIntValidator(1, 1000, ));