QValidator(3qt)

NAME
QValidator - Validation of input text

#include <qvalidator.h>

Inherits QObject.

Inherited by QDoubleValidator and QIntValidator.

  Public Members
QValidator ( QWidget * parent, const char * name = 0 )
~QValidator ()
enum State { Invalid, Intermediate, Valid=Intermediate, Acceptable }
virtual State validate ( QString &, int & ) const
virtual void fixup ( QString & ) const

DESCRIPTION
The QValidator class provides validation of input text.

The class itself is abstract; two subclasses provide rudimentary numeric range checking.

The class includes two virtual functions, validate() and fixup().

validate() is pure virtual, so it must be implemented by every subclass. It returns Invalid, Intermediate or Acceptable depending on whether its argument is valid (for the class' definition of valid).

The three states require some explanation. An Invalid string is clearly invalid. Intermediate is less obvious - the concept of validity is slippery when the string is incomplete (still being edited). QValidator defines Intermediate as the property of a string that it is neither clearly invalid or acceptable as a final result. Acceptable means that the string is acceptable as a final result. One might say that any string that is a plausible intermediate state during entry of an Acceptable string is Intermediate.

Here are some examples:

1   For a line edit that accepts integers from 0 to 999 inclusive, 42 and 666 are Acceptable, the empty string and 1114 are Intermediate and asdf is Invalid.

2   For an editable combo box that accepts URLs, any well-formed URL is Acceptable, "http://www.trolltech.com/," is Intermediate (it can be a cut-and-paste job that accidentally took in a comma at the end), the empty string is valid (the user might select and delete all of the text in preparation to entering a new URL), and" http:///./" is Invalid.

3   For a spin box that accepts lengths, "11cm" and "1in" are Acceptable, "11" and the empty string are Intermediate, and" http://www.trolltech.com" and "hour" are Invalid.

fixup() is provided for validators that can repair some or all user errors. The default does nothing. QLineEdit, for example, will call fixup() if the user presses Return and the content is not currently valid, in case fixup() can do magic. This allows some Invalid strings to be made Acceptable, too, spoiling the muddy definition above even more.

QValidator is generally used with QLineEdit, QSpinBox and QComboBox.

  Member Type Documentation

QValidator::State
This enum type defines the states in which a validated string can be. There are currently three states:

Invalid - the string is clearly invalid.

Intermediate - the string is a plausible intermediate value during editing.

Acceptable - acceptable as a final result.

The state Valid has been renamed Intermediate. The old name confused too many people and is now obsolete.

MEMBER FUNCTION DOCUMENTATION

QValidator::QValidator ( QWidget * parent, const char * name = 0 )
Sets up the internal data structures used by the validator. At the moment there aren't any.

QValidator::~QValidator ()
Destroys the validator, freeing any storage and other resources used.

void QValidator::fixup ( QString & input ) const [virtual]
Attempts to change input to be valid according to this validator's rules. Need not result in a valid string - callers of this function must re-test afterwards. The default does nothing.

Reimplementations of this function can change input even if they do not produce a valid string. For example an ISBN validator might want to delete every character except digits and "-", even if the result is not a valid ISBN, and a last-name validator might want to remove white space from the start and end of the string, even if the resulting string is not in the list of known last names.

QValidator::State QValidator::validate( QString & input, int & pos ) const
This pure virtual function returns Invalid if input is invalid according to this validator's rules, Intermediate if it is likely that a little more editing will make the input acceptable (e.g. the user types '4' into a widget which accepts 10-99) and Acceptable if the input is completely acceptable.

The function can change input and pos (the cursor position) if it wants to.

SEE ALSO
http://doc.trolltech.com/qvalidator.html http://www.trolltech.com/faq/tech.html

COPYRIGHT
Copyright 1992-2001 Trolltech AS, http://www.trolltech.com. See the license file included in the distribution for a complete license statement.

AUTHOR
Generated automatically from the source code.

BUGS
If you find a bug in Qt, please report it as described in http://doc.trolltech.com/bughowto.html. Good bug reports make our job much simpler. Thank you.

In case of content or formattting problems with this manual page, please report them to qt-bugs@trolltech.com. Please include the name of the manual page (qvalidator.3qt) and the Qt version (2.3.7).