Reputation: 11
I'm new to PyQt5 and trying to implement some code in Python and PyQt5 to validate a QLineEdit field's user input.
The QIntValidator 'bottom' integer value is set to 100 and the 'top' to 200. When inputting data to the field, values over 200 can't be input but values under 100 can. Have I missed out some steps or misunderstood this validator's functioning? This is my first question on stackoverflow - hope I've described the problem clearly. Any help much appreciated!
# Qt5_IntValidator.py
# Qt5 IntValidator testing
import sys
from PyQt5.Qt import QApplication
from PyQt5.QtCore import QRegExp
from PyQt5.QtGui import QRegExpValidator, QIntValidator
from PyQt5.QtWidgets import QWidget, QLineEdit
class MyWidget_IntValidator(QWidget):
def __init__(self, parent=None):
super(QWidget, self).__init__(parent)
self.le_input = QLineEdit(self)
self.le_input.setMaxLength(4)
self.input_validator = QIntValidator(self.le_input)
#self.input_validator = QIntValidator(100, 200, self.le_input)
self.input_validator.setRange(100, 200)
#self.input_validator.setBottom(100)
#self.input_validator.setTop(200)
print(f"Bottom: {QIntValidator.bottom(self.input_validator)}")
print(f"Top: {QIntValidator.top(self.input_validator)}\n")
self.le_input.setValidator(self.input_validator)
self.le_input.textChanged[str].connect(self.le_textChanged)
def le_textChanged(self, text):
self.input_text = text
text_status = QIntValidator.validate(self.input_validator, self.input_text, 0)
print(f"Status: {text_status}")
if text_status[0] == 2: # 2=Acceptable, 1=Intermediate, 0=Invalid.
print("Acceptable!")
elif text_status[0] == 1:
print("Intermediate..")
else:
print("Invalid:(")
if __name__ == '__main__':
a = QApplication(sys.argv)
w = MyWidget_IntValidator()
w.show()
a.exec()
Upvotes: 1
Views: 935