大家好,我想出了另一个问题,这是我的示例代码:
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 ()
但问题仍然存在,这并不能证实我的年龄。
答案 0 :(得分:0)
你最好的选择可能是QValidator
这样的子类:
qagevalidator.h
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;
}