This is a two part question about input validation with one specific and another more general component.
The specific:
While researching the topic, I found THIS on Regular Expressions. I realize that the code in this post is using PyQt4. However I wanted to get this working with PyQt5, since I had already started my project with it. (Obviously blindly - I can only find C++ documentation for it)
This is what I tried:
# somewhere above:
self.le_input = QtWidgets.QLineEdit()
# at some point validate_input gets called:
# more on that in the second part of this question
def validate_input(self):
reg_ex = QtCore.QRegExp(""[0-9]+.?[0-9]{,2}"")
input_validator = QtGui.QRegExpValidator(reg_ex, self.le_input.text())
self.le_input.setValidator(input_validator)
When I run the code I get the following Error:
QRegExpValidator(parent: QObject = None): argument 1 has unexpected type 'QRegExp' QRegExpValidator(QRegExp, parent: QObject = None): argument 2 has unexpected type 'str'
Aren't these exactly the required arguments? Does anyone know how to get this working?
The general:
What is an effective way to implement live validation with PyQt in Python?
At the moment I would use:
self.le_input.textChanged.connect(self.validate_input)
This does work, but as soon as I try to connect two QtLineEdits, that affect each other, to the same slot, things stop working because "textChanged" gets called by both of them at the same time.
Use case: Two input fields: Amount before TAX
and Amount after TAX
- and whichever you enter automatically fills the other one while typing.
First validation, then calculation, then output to the other field.
Many thanks in advance! Any help is highly appreciated!