QValidator¶
The
QValidatorclass provides validation of input text. More…

Inherited by: QDoubleValidator, QIntValidator, QRegExpValidator
Synopsis¶
Detailed Description¶
The class itself is abstract. Two subclasses,
QIntValidatorandQDoubleValidator, provide basic numeric-range checking, andQRegExpValidatorprovides general checking using a custom regular expression.If the built-in validators aren’t sufficient, you can subclass
QValidator. The class has two virtual functions:validate()andfixup().
validate()must be implemented by every subclass. It returnsInvalid,IntermediateorAcceptabledepending on whether its argument is valid (for the subclass’s definition of valid).These three states require some explanation. An
Invalidstring is clearly invalid.Intermediateis less obvious: the concept of validity is difficult to apply when the string is incomplete (still being edited).QValidatordefinesIntermediateas the property of a string that is neither clearly invalid nor acceptable as a final result.Acceptablemeans that the string is acceptable as a final result. One might say that any string that is a plausible intermediate state during entry of anAcceptablestring isIntermediate.Here are some examples:
For a line edit that accepts integers from 10 to 1000 inclusive, 42 and 123 are
Acceptable, the empty string and 5 areIntermediate, and “asdf” and 1114 isInvalid.For an editable combobox that accepts URLs, any well-formed URL is
Acceptable, “http://example.com/,” isIntermediate(it might be a cut and paste action that accidentally took in a comma at the end), the empty string isIntermediate(the user might select and delete all of the text in preparation for entering a new URL) and “http:///./” isInvalid.For a spin box that accepts lengths, “11cm” and “1in” are
Acceptable, “11” and the empty string areIntermediate, and “http://example.com” and “hour” areInvalid.
fixup()is provided for validators that can repair some user errors. The default implementation does nothing.QLineEdit, for example, will callfixup()if the user presses Enter (or Return) and the content is not currently valid. This allows thefixup()function the opportunity of performing some magic to make anInvalidstringAcceptable.A validator has a locale, set with
setLocale(). It is typically used to parse localized data. For example,QIntValidatorandQDoubleValidatoruse it to parse localized representations of integers and doubles.
QValidatoris typically used withQLineEdit,QSpinBoxandQComboBox.
-
class
QValidator([parent=None])¶ - param parent
QObject
Sets up the validator. The
parentparameter is passed on to theQObjectconstructor.
-
PySide2.QtGui.QValidator.State¶ This enum type defines the states in which a validated string can exist.
Constant
Description
QValidator.Invalid
The string is clearly invalid.
QValidator.Intermediate
The string is a plausible intermediate value.
QValidator.Acceptable
The string is acceptable as a final result; i.e. it is valid.
-
PySide2.QtGui.QValidator.changed()¶
-
PySide2.QtGui.QValidator.fixup(arg__1)¶ - Parameters
arg__1 – unicode
This function attempts to change
inputto be valid according to this validator’s rules. It 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
inputeven 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 still not a valid ISBN; a surname validator might want to remove whitespace from the start and end of the string, even if the resulting string is not in the list of accepted surnames.
-
PySide2.QtGui.QValidator.locale()¶ - Return type
QLocale
Returns the locale for the validator. The locale is by default initialized to the same as QLocale().
See also
setLocale()QLocale()
-
PySide2.QtGui.QValidator.setLocale(locale)¶ - Parameters
locale –
QLocale
Sets the
localethat will be used for the validator. Unless has been called, the validator will use the default locale set withsetDefault(). If a default locale has not been set, it is the operating system’s locale.See also
locale()setDefault()
-
PySide2.QtGui.QValidator.validate(arg__1, arg__2)¶ - Parameters
arg__1 – unicode
arg__2 –
int
- Return type
PyObject
This virtual function returns
Invalidifinputis invalid according to this validator’s rules,Intermediateif it is likely that a little more editing will make the input acceptable (e.g. the user types “4” into a widget which accepts integers between 10 and 99), andAcceptableif the input is valid.The function can change both
inputandpos(the cursor position) if required.
© 2018 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.