#include <qvalidator.h>
Inheritance diagram for QIntValidator:


Example of use:
QValidator *validator = new QIntValidator(100, 999, this); QLineEdit *edit = new QLineEdit(this); // the edit lineedit will only accept integers between 100 and 999 edit->setValidator(validator);
Below we present some examples of validators. In practice they would normally be associated with a widget as in the example above.
QString str; int pos = 0; QIntValidator v(100, 999, this); str = "1"; v.validate(str, pos); // returns Intermediate str = "12"; v.validate(str, pos); // returns Intermediate str = "123"; v.validate(str, pos); // returns Acceptable str = "678"; v.validate(str, pos); // returns Acceptable str = "1234"; v.validate(str, pos); // returns Invalid str = "-123"; v.validate(str, pos); // returns Invalid str = "abc"; v.validate(str, pos); // returns Invalid str = "12cm"; v.validate(str, pos); // returns Invalid
The minimum and maximum values are set in one call with setRange(), or individually with setBottom() and setTop().
Definition at line 66 of file qvalidator.h.
Public Member Functions | |
| QIntValidator (QObject *parent) | |
| QIntValidator (int bottom, int top, QObject *parent) | |
| ~QIntValidator () | |
| QValidator::State | validate (QString &, int &) const |
| void | setBottom (int) |
| void | setTop (int) |
| virtual void | setRange (int bottom, int top) |
| int | bottom () const |
| int | top () const |
Private Attributes | |
| int | b |
| int | t |
| QIntValidator::QIntValidator | ( | QObject * | parent | ) | [explicit] |
Constructs a validator with a parent object that accepts all integers.
Definition at line 239 of file qvalidator.cpp.
00240 : QValidator(parent) 00241 { 00242 b = INT_MIN; 00243 t = INT_MAX; 00244 }
| QIntValidator::QIntValidator | ( | int | minimum, | |
| int | maximum, | |||
| QObject * | parent | |||
| ) |
Constructs a validator with a parent, that accepts integers from minimum to maximum inclusive.
Definition at line 252 of file qvalidator.cpp.
00254 : QValidator(parent) 00255 { 00256 b = minimum; 00257 t = maximum; 00258 }
| QIntValidator::~QIntValidator | ( | ) |
| QValidator::State QIntValidator::validate | ( | QString & | input, | |
| int & | pos | |||
| ) | const [virtual] |
Returns Acceptable if the input is an integer within the valid range, Intermediate if the input is an integer outside the valid range and Invalid if the input is not an integer.
Note: If the valid range consists of just positive integers (e.g. 32 to 100) and input is a negative integer then Invalid is returned.
int pos = 0; s = "abc"; v.validate(s, pos); // returns Invalid s = "5"; v.validate(s, pos); // returns Intermediate s = "50"; v.validate(s, pos); // returns Acceptable
By default, the pos parameter is not used by this validator.
Implements QValidator.
Definition at line 331 of file qvalidator.cpp.
References QValidator::Acceptable, b, QString::contains(), QValidator::Intermediate, QValidator::Invalid, QString::isEmpty(), and t.
00332 { 00333 if (input.contains(QLatin1Char(' '))) 00334 return Invalid; 00335 if (input.isEmpty() || (b < 0 && input == QLatin1String("-"))) 00336 return Intermediate; 00337 bool ok; 00338 int entered = input.toInt(&ok); 00339 if (!ok || (entered < 0 && b >= 0)) { 00340 return Invalid; 00341 } else if (entered >= b && entered <= t) { 00342 return Acceptable; 00343 } else { 00344 if (entered >= 0) 00345 return (entered > t) ? Invalid : Intermediate; 00346 else 00347 return (entered < b) ? Invalid : Intermediate; 00348 } 00349 }
Here is the call graph for this function:

| void QIntValidator::setBottom | ( | int | ) |
Definition at line 370 of file qvalidator.cpp.
References setRange(), and top().
Here is the call graph for this function:

| void QIntValidator::setTop | ( | int | ) |
Definition at line 381 of file qvalidator.cpp.
References bottom(), and setRange().
Here is the call graph for this function:

| void QIntValidator::setRange | ( | int | bottom, | |
| int | top | |||
| ) | [virtual] |
Sets the range of the validator to only accept integers between bottom and top inclusive.
Definition at line 357 of file qvalidator.cpp.
Referenced by setBottom(), and setTop().
| int QIntValidator::bottom | ( | ) | const [inline] |
Definition at line 83 of file qvalidator.h.
References b.
Referenced by setTop().
00083 { return b; }
| int QIntValidator::top | ( | ) | const [inline] |
Definition at line 84 of file qvalidator.h.
References t.
Referenced by setBottom().
00084 { return t; }
int QIntValidator::b [private] |
Definition at line 95 of file qvalidator.h.
Referenced by QIntValidator(), setRange(), and validate().
int QIntValidator::t [private] |
Definition at line 96 of file qvalidator.h.
Referenced by QIntValidator(), setRange(), and validate().
1.5.1