QDoubleValidator 用于双精度的数据的验证,但4.7.4 以下版本几乎不起作用,百度一下,也发现网友也遇到此类问题,经研究得出以下解决方案:
从QDoubleValidator派生新的类MyDoubleValidator,MyDoubleValidator的简要实现代码如下:
class MyDoubleValidator: public QDoubleValidator
{
public:
explicit MyDoubleValidator(QObject * parent = 0)
:QDoubleValidator(parent)
{
}
virtual State validate(QString &str, int &i) const
{
if (str.isEmpty())
{
return QValidator::Intermediate;
}
int a = 1;
bool cOK = false;
double val = str.toDouble(&cOK);
if (!cOK)
{
return QValidator::Invalid;
}
int dotPos = str.indexOf(".");
if (dotPos > 0)
{
if (str.right(str.length() - dotPos-1).length() > decimals())
{
return QValidator::Invalid;
}
}
if (val< top() && val > bottom())
{
return QValidator::Acceptable;
}
return QValidator::Invalid;
}
};
主要是重写 validate 接口