众所周知QIntValidator和QDoubleValidator存在缺陷,最明显的一点就是数据范围不准确。
1
| auto intVaildator = new QIntValidator(1,199);
|
解决方法:重载validate函数
1
| [override virtual] QValidator::State QIntValidator::validate(QString &input, int &pos) const
|
具体实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| QValidator::State IntValidator::validate(QString &input, int &pos) const { const State originalRes = QIntValidator::validate(input, pos); if (originalRes != Intermediate || input.isEmpty()) { return originalRes; }
const auto extracted = locale().toLongLong(input); if (extracted > 0) { if (extracted > top() && -extracted < bottom()) { return Invalid; } } else if (extracted < bottom()) { return Invalid; } return originalRes; }
|