当前位置:   article > 正文

PyQt5 QLineEdit校验器限制输入_python qlineedit qdoublevalidator 输入范围限制无效

python qlineedit qdoublevalidator 输入范围限制无效

校验器含义

Qvalidator 校验器用于检验用户输入的数据的合法性。如果一个输入框设置了校验器,到时用户在文本框中输入内容时,首先会将内容传递给验证器进行验证,如果输入框结束输入后,上述的验证状态并非有效,则不允许输入。

编辑框或者其他输入控件 可以通过类似于SetValidator来指定验证器。

QValidator 是一个抽象类,有一些子类:

  • QIntValidator设置合法 int
    • 可以设置其range,setRange()
  • QDoubleValidator 设置合法 Double
    • 可以设置range setRange, ,小数点后位数setDecimals
  • QRegExpValidator 是用来结合正则表达式,判断合法性
    -利用正则表达式自由发挥
    • QRegExp, PyQt中的正则表达式类,设置正则表达式对象
    • setRegExp,指定校验器的正则表达式

系统校验器子类举例

from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIntValidator, QDoubleValidator, QRegExpValidator
from PyQt5.QtCore import QRegExp
import sys


class QLineEditorValidator(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        self.setWindowTitle("校验器")
        form_layout = QFormLayout()
        int_line = QLineEdit()
        double_line = QLineEdit()
        re_line = QLineEdit()
        int_line.setPlaceholderText("整形数据")
        double_line.setPlaceholderText("浮点数据")
        re_line.setPlaceholderText("正则数据")
        form_layout.addRow("整形数据", int_line)
        form_layout.addRow("浮点数据", double_line)
        form_layout.addRow("正则数据", re_line)

        int_validator = QIntValidator()
        int_validator.setRange(1, 10)

        double_validator = QDoubleValidator()
        double_validator.setRange(-360, 360)
        double_validator.setDecimals(2)

        re_validator = QRegExpValidator()
        reg = QRegExp("^[a-zA-Z0-9]+$")
        re_validator.setRegExp(reg)

        int_line.setValidator(int_validator)
        double_line.setValidator(double_validator)
        re_line.setValidator(re_validator)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = QLineEditorValidator()
    win.show()
    sys.exit(app.exec_())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

自定义抽象类

  • 由于 QValidator 有一个纯虚函数 validate.所以必须要写一个类来继承,然后实现这个纯虚函数才行。
def validate(self, input_str, input_int):
        # input_str是输入的内容
        # input_int是光标的位置
        ....
  • 1
  • 2
  • 3
  • 4
  • 返回State有三种状态
    • QValidator.Invalid:输入是不允许的
    • QValidator.Intermediate ,处在中间状态,还无法判断。需要后续。
    • QValidator.Acceptable,输入是允许的
from PyQt5.Qt import *
import sys


class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("验证器的使用")
        self.resize(500, 500)
        self.setup_ui()

    def setup_ui(self):
        le = QLineEdit()
        le.move(100, 100)
        # 18-180
        # QValidator不能直接用,而是用一个子类来继承,然后实体化
        # 因此上面写了一个新的AgeValidator继承于QValidator
        validator1 = ScoreValidator()
        le.setValidator(validator1)

        le2 = QLineEdit()
        le2.move(200, 200)
        fm_layout = QFormLayout(self)
        fm_layout.addRow("输入年龄", le)
        fm_layout.addRow("打酱油行", le2)


class ScoreValidator(QValidator):
    def validate(self, input_str, input_int):
        # input_str: 输入框中的已有内容
        # input_int: 光标的位置
        print(f"当前数值是:{input_str}, 是第{input_int}个字符")

        try:
            if 60 <= int(input_str) <= 100:
                # 需要有一个返回值
                return QValidator.Acceptable, input_str, input_int  # 返回验证通过
            elif 5 <= int(input_str) < 60:
                return QValidator.Intermediate, input_str, input_int  # 返回验证中间状态,不做响应
            else:
                return QValidator.Invalid, input_str, input_int  # 返回验证不通过
        except:
            if len(input_str) == 0:
                return QValidator.Intermediate, input_str, input_int  # 返回验证中间状态,不做响应
            return QValidator.Invalid, input_str, input_int  # 返回验证不通过


# 方便在模板里进行调试
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/299791
推荐阅读
相关标签
  

闽ICP备14008679号