2

These are the 2 questions(both can be solved by InputMask?)

  1. I want to restrict the user input to 16 characters only
  2. In a field like 'Age/ID', I would like the user's input to be integer only, if the user enters a string it must not be accepted or the user must not be able to type in a string in the first place.

I'm not sure how I can implement the first part in real-time,i.e., the user types a max of 16, nothing beyond 16 appears.

This is my code(not working) for the 2nd part of the question:

self.onlyInt = QIntValidator()
self.lineEdit_15.setValidator(self.onlyInt)
det15=str(self.lineEdit_15.text())
list_val.append(det15)
Usman Maqbool
  • 3,351
  • 10
  • 31
  • 48
Ashksta
  • 61
  • 2
  • 7
  • http://pyqt.sourceforge.net/Docs/PyQt4/qlineedit.html#setMaxLength This can be used to implement the first part in "real time" – APorter1031 Nov 27 '17 at 14:18
  • 1
    As for the second part, refer to this question; https://stackoverflow.com/questions/15829782/how-to-restrict-user-input-in-qlineedit-in-pyqt – APorter1031 Nov 27 '17 at 14:19
  • I would highly recommend using a QSpinBox for the integer input field. – bugmenot123 Aug 01 '22 at 14:20

1 Answers1

2

To solve the first question we only have to establish a maximum size:

self.lineedit_15.setMaxLength(16)

In contrast the second QIntValidator question only works up to a maximum equal to 2147483647 since it is the maximum integer: 2**31-1, The solution is to use regular expressions:

rx = QRegExp("\d+")
self.lineedit_15.setValidator(QRegExpValidator(rx))
eyllanesc
  • 235,170
  • 19
  • 170
  • 241