I am trying to use QValidator
descendants (actually in PyQt5, but that shouldn't matter) to validate a series of line-edits.
A small excerpt is:
class IPv4(QWidget):
def __init__(self):
super(IPv4, self).__init__()
uic.loadUi('ipv4.ui', self)
self.address.inputMask = ''
rx = QRegularExpression(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
self.address.setValidator(QRegularExpressionValidator(rx, self.address))
self.netmask.setValidator(QRegularExpressionValidator(rx, self.netmask))
self.gateway.setValidator(QRegularExpressionValidator(rx, self.gateway))
self.broadcast.setValidator(QRegularExpressionValidator(rx, self.broadcast))
self.dns1.setValidator(QRegularExpressionValidator(rx, self.dns1))
self.dns2.setValidator(QRegularExpressionValidator(rx, self.dns2))
self.on_dhcp_clicked(self.dhcp.isChecked())
This works as advertised, but the user gets no feedback, since trying to input "wrong" characters simply discards them.
I didn't find any way to give feedback beside hooking into the QLineEdit.textChanged
signal and doing validation "manually" (i.e.: without setting a validator, otherwise on error text
won't change and no signal will be emitted). The preferred feedback would be to change the line-edit's border-color.
This somehow defeats the purpose of the validator itself. It seems I'm missing something, since I can't see how to trigger feedback from QValidator
.
What is the "standard way" to handle this?
QLineEdit.validator
takes precedence, and thuson_textChanged()
is not called at all on invalid input. Your code actually distinguish betweenIntermediate
andAcceptable
. Still no feedback (not even a beep) for trulyInvalid
entry. Side question: why did You edit tags? I don't think this is specific to pyqt; it looks like a design issue in originalQValidator
class. – Cheeseparing