User Input Validation in PyQt5 and Python

后端 未结 2 1956
心在旅途
心在旅途 2021-01-18 20:09

This is a two part question about input validation with one specific and another more general component.

The specific:

While researching the

2条回答
  •  孤独总比滥情好
    2021-01-18 20:46

    Either methods seems to work well with input validation except for textChanged() signal when two lineEdits which affects each other when using the same slot function .However I have a comment on QRegExpValidator, it may scare the user to think their keyboard is not working since trying to type anything not allowed shows nothing(no interaction whatsoever) while using textChanged() signal can allow some interaction using some statements in the slot function. Any thoughts on my view above? How can I address this? @eyllanesc

    I have made my conclusion by trying this code example shared above and many other examples I may not be able to share:

    from PyQt5.Qt import QApplication
    from PyQt5.QtCore import QRegExp
    from PyQt5.QtGui import QRegExpValidator
    from PyQt5.QtWidgets import QWidget, QLineEdit
    
    import sys
    
    class MyWidget(QWidget):
        def __init__(self, parent=None):
            super(QWidget, self).__init__(parent)
            self.le_input = QLineEdit(self)
    
            reg_ex = QRegExp("[0-9]+.?[0-9]{,2}")
            input_validator = QRegExpValidator(reg_ex, self.le_input)
            self.le_input.setValidator(input_validator)
    
    if __name__ == '__main__':
        a = QApplication(sys.argv)
    
        w = MyWidget()
        w.show()
    
        a.exec()
    

提交回复
热议问题