嗨,大家好,我想出了另一个问题,这是我的示例代码:
QValidator *m_validator = new QIntValidator(18, 75, this);
QLineEdit *m_edit = new QLineEdit(this);
m_edit->setValidator(m_validator);
这里的问题是,我不想去SpinBox
,我想只验证这个lineEdit
,但这里范围不起作用范围意味着如果我给1它也允许我知道lineEdit
工作键盘输入单字符。但我需要从lineEdit
获得整数并验证它我尝试SIGNAL
:
void editingFinished ()
但仍然存在问题仍然没有证实我的年龄。
你最好的选择可能是像这样继承QValidator
:
Kagewalidatorkh
class QAgeValidator : public QValidator
{
Q_OBJECT
Q_DISABLE_COPY(QAgeValidator )
quint16 low, high;
public:
explicit QAgeValidator(quint16 low, quint16 high, QObject *parent = 0);
virtual ~QAgeValidator();
virtual QValidator::State validate(QString &, int &) const;
virtual void fixup(QString &) const;
};
并重新实现虚拟功能,如下所示:
QValidator::State QAgeValidator::validate(QString &input, int &) const
{
if (input.isEmpty())
return Acceptable;
quint16 i = input.toInt();
if (!isdigit(i) || (i < low || i > high))
return Invalid;
return Acceptable;
}
这是您应该进行年龄验证的地方:
void QAgeValidator::fixup(QString &input) const
{
QString final;
for (int i = 0; i < input.length(); ++i) {
QChar c = input.at(i);
if (c.isDigit())
final += c;
}
input = final;
}