Although Qt provides a wide range of ready-made input widgets for things such as dates and numbers, we may find sometimes that we need a widget with very specific constraints on its input values. Such input constraints can be created using the QValidator class.
The workflow is like this:
- Create a custom validator class by subclassing QtGui.QValidator
- Override the validate() method with our validation logic
- Assign an instance of our custom class to a widget's validator property
Once assigned to an editable widget, the validate() method will be called every time the user updates the value of the widget (for example, every keystroke in QLineEdit) and will determine whether the input is accepted.