1

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()

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
tjm_777
  • 11
  • 2
  • Your description is unclear, I can enter 15 without problems. – eyllanesc Nov 29 '19 at 22:41
  • Thanks for the comment. I want the user to only be able to input an integer between 100 and 200 inclusive. – tjm_777 Nov 30 '19 at 08:17
  • 1
    @Pytim That makes no sense. How are users going to enter `123` if they can't type `12` first? I suggest you use a `QSpinBox` instead. – ekhumoro Nov 30 '19 at 16:24
  • @ekhumoro Thanks, it's clearer now how QIntValidator works. I will try a QSpinBox as you suggest. – tjm_777 Nov 30 '19 at 21:30
  • @ekhumoro QSpinBox for an integer range of 100-200 works if you use the 'up' and 'down' buttons. However, if you directly input a number it will allow '000' to '099' inclusive. Also, going back to the QIntValidator example, I agree about a user inputting 12 and the validator not kicking in. What about inputting 012 in the three digit field though? Many thanks for any insights! – tjm_777 Dec 09 '19 at 15:00
  • @Pytim A spinbox does not really allow values outside of the range. Pressing enter (or switching focus) will revert to the previous value if the current value is invalid. Allowing manual entry means the user ***must*** have the freedom to type partial values and use all the other standard editing operations. If you don't want to allow manual entry, you can just do `spinbox.lineEdit().setReadOnly(True)`. – ekhumoro Dec 09 '19 at 17:01
  • @ekhumoro Thanks for the info on a spinbox reverting to its previous value if the current value is invalid when leaving the box. I will now progress with this rather than the QIntValidator route. – tjm_777 Dec 11 '19 at 15:11

0 Answers0