Skip to content

TextLengthValidator

Qt Base Class: QValidator

Signature: QValidator(self, parent: Optional[PySide6.QtCore.QObject] = None) -> None

Base classes

Name Children Inherits
Validator
prettyqt.gui.validator

⋔ Inheritance diagram

graph TD
  1473367017936["validators.TextLengthValidator"]
  1473245568976["gui.Validator"]
  1473245536768["gui.ValidatorMixin"]
  1473299815024["core.ObjectMixin"]
  140713234304496["builtins.object"]
  1473289208592["QtGui.QValidator"]
  1473288842240["QtCore.QObject"]
  1473291690208["Shiboken.Object"]
  1473245568976 --> 1473367017936
  1473245536768 --> 1473245568976
  1473299815024 --> 1473245536768
  140713234304496 --> 1473299815024
  1473289208592 --> 1473245568976
  1473288842240 --> 1473289208592
  1473291690208 --> 1473288842240
  140713234304496 --> 1473291690208

🛈 DocStrings

Bases: Validator

Validator which checks whether given text has a specific length.

Source code in prettyqt\validators\textlengthvalidator.py
class TextLengthValidator(gui.Validator):
    """Validator which checks whether given text has a specific length."""

    ID = "text_length"

    def __init__(
        self,
        minimum: int | None = None,
        maximum: int | None = None,
        parent: core.QObject | None = None,
    ):
        super().__init__(parent)
        self._minimum = 0 if minimum is None else minimum
        self._maximum = sys.maxsize if maximum is None else maximum

    def set_range(self, minimum: int | None, maximum: int | None):
        self._minimum = 0 if minimum is None else minimum
        self._maximum = sys.maxsize if maximum is None else maximum

    def get_range(self) -> tuple[int | None, int | None]:
        return (self._minimum, self._maximum)

    def set_minimum(self, minimum: int | None):
        self._minimum = 0 if minimum is None else minimum

    def get_minimum(self) -> int:
        return self._minimum

    def set_maximum(self, maximum: int | None):
        self._maximum = sys.maxsize if maximum is None else maximum

    def get_maximum(self) -> int:
        return self._maximum

    def validate(self, text: str, pos: int):
        if self._minimum <= len(text) <= self._maximum:
            return self.State.Acceptable, text, len(text)
        if len(text) <= self._maximum:
            return self.State.Intermediate, text, len(text)
        return self.State.Invalid, text, len(text)

    minimum = core.Property(
        int,
        get_minimum,
        set_minimum,
        doc="Minimum text length",
    )
    maximum = core.Property(
        int,
        get_maximum,
        set_maximum,
        doc="Maximum text length",
    )

⌗ Property table

Qt Property Type Doc
objectName QString
minimum int Minimum text length
maximum int Maximum text length

Validator ID: text_length