I have a Line Edit widget with a QIntValidator:
number_textbox = QLineEdit()
number_textbox.setText("0")
number_textbox.setValidator(QIntValidator())
In addition, I am storing the old value of the textbox in a dict:
old_val[uid_key] = 0
I would like to be able to, when the textbox is empty, replace the text in the textbox with the old value if the user clicks outside the box or presses enter:
number_textbox.editingFinished.connect(lambda: self.editing_finished_function(number_textbox, old_val[uid_key]))
def editing_finished_function(self, textbox, old_val):
if textbox.text() == '':
textbox.setText(old_val)
else:
old_val = textbox.text()
Unfortunately, the QIntValidator() seems to consider empty values as invalid, and never fires my function. Is there a way to do what I'm looking for here?
from docs:
void QLineEdit::editingFinished()
And QIntValidator doesn't seem to consider empty string to be valid. So I suggest you create your own validator using
QRegExpValidator
Below is the validator that accepts both numeric and empty string
Full example: