QIntValidator Class Reference

#include <qvalidator.h>

Inheritance diagram for QIntValidator:

Inheritance graph
[legend]
Collaboration diagram for QIntValidator:

Collaboration graph
[legend]
List of all members.

Detailed Description

The QIntValidator class provides a validator that ensures a string contains a valid integer within a specified range.

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().

See also:
QDoubleValidator, QRegExpValidator, {Line Edits Example}

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


Constructor & Destructor Documentation

QIntValidator::QIntValidator ( QObject parent  )  [explicit]

Constructs a validator with a parent object that accepts all integers.

Definition at line 239 of file qvalidator.cpp.

References b, and t.

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.

References b, and t.

00254     : QValidator(parent)
00255 {
00256     b = minimum;
00257     t = maximum;
00258 }

QIntValidator::~QIntValidator (  ) 

Destroys the validator.

Definition at line 299 of file qvalidator.cpp.

00300 {
00301     // nothing
00302 }


Member Function Documentation

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().

00371 {
00372     setRange(bottom, top());
00373 }

Here is the call graph for this function:

void QIntValidator::setTop ( int   ) 

Definition at line 381 of file qvalidator.cpp.

References bottom(), and setRange().

00382 {
00383     setRange(bottom(), top);
00384 }

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.

References b, and t.

Referenced by setBottom(), and setTop().

00358 {
00359     b = bottom;
00360     t = top;
00361 }

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; }


Member Data Documentation

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().


The documentation for this class was generated from the following files:
Generated on Thu Mar 15 18:04:29 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1