【问题标题】:Why does QRegExpValidator always return 2为什么 QRegExpValidator 总是返回 2
【发布时间】:2020-08-18 16:42:56
【问题描述】:

我的QRegExpValidator 总是认为我的输入是Acceptable - 它不应该是。在 gui.py 中有一个名为 `

的QLineEdit 对象
from gui import Ui_Dialog
from PyQt5.QtWidgets import QDialog, QApplication
from PyQt5 import QtCore
from PyQt5.QtGui import QRegExpValidator
from PyQt5.QtCore import QRegExp
import sys


class AppWindow(QDialog):   
    def __init__(self):
        super().__init__()
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.show()

        self.ui.number.textChanged[str].connect(self.validate_number)

    def validate_number(self):
        regex = QRegExp("^[0-9]{3}$")
        tmp = QRegExpValidator(regex, self.ui.number)
        print(tmp.Acceptable)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = AppWindow()
    w.show()
    sys.exit(app.exec_())

无论我尝试什么,输出总是2。我希望我输入任何内容,如果它是一个 3 位数字,则该函数返回 True(或 Acceptable)。 我试图按照这条路径解释here,我错过了什么?

【问题讨论】:

    标签: python pyqt pyqt5 qlineedit qvalidator


    【解决方案1】:

    似乎 OP 不了解 QValidator 如何与 QLineEdit 一起工作。

    逻辑是使用方法 setValidator 将 QValidator 设置为 QLineEdit,其中已建立连接,因此每次更改 QLineEdit 的文本时,都会调用“验证”方法,该方法返回状态,新的位置和新文本(如果需要更正)。

    class AppWindow(QDialog):   
        def __init__(self):
            super().__init__()
            self.ui = Ui_Dialog()
            self.ui.setupUi(self)
            self.show()
    
            regex = QRegExp("^[0-9]{3}$")
            validator = QRegExpValidator(regex, self.ui.number)
            self.ui.number.setValidator(validator)

    另一方面,由于 "tmp" 是一个 QValidator,那么 tmp.Acceptable 等价于 QValidator.Acceptable 并且在打印它时,会获得该枚举的数值。

    如果您想分析验证器状态的值,那么您有以下选择:

    • 覆盖验证(最推荐):

      class RegExpValidator(QRegExpValidator):
          def validate(self, text, position):
              state, new_text, new_position = super().validate(text, position)
              print(state)
              return state, new_text, new_position
      
      
      class AppWindow(QDialog):   
          def __init__(self):
              super().__init__()
              self.ui = Ui_Dialog()
              self.ui.setupUi(self)
              self.show()
      
              regex = QRegExp("^[0-9]{3}$")
              validator = RegExpValidator(regex, self.ui.number)
              self.ui.number.setValidator(validator)
      
    • 调用验证方法:

      class AppWindow(QDialog):
          def __init__(self):
              super().__init__()
              self.ui = Ui_Dialog()
              self.ui.setupUi(self)
              self.show()
      
              self.ui.number.textChanged[str].connect(self.validate_number)
      
          def validate_number(self):
              regex = QRegExp("^[0-9]{3}$")
              tmp = QRegExpValidator(regex, self.ui.number)
              state, new_text, new_position = tmp.validate(
                  self.ui.number.text(), self.ui.number.cursorPosition()
              )
              print(state)
      

    【讨论】:

    • 是的,我理解验证器如何工作错误的想法。谢谢你教育我!你的第二种方法正是我想要的。
    猜你喜欢
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-12
    • 2016-09-16
    • 1970-01-01
    相关资源
    最近更新 更多