How to restrict user input in QLineEdit in pyqt

Rao picture Rao · Apr 5, 2013 · Viewed 27.2k times · Source

I have a QLineEdit and i want to restrict QLineEdit to accept only integers. It should work like inputmask. But I dont want to use inputmask, because if user clicks on QLineEdit cursor will be at the position where mouse was clicked. and user need to navigate to 0 position and type what eve he wants.

Is there any alternate for this.

Answer

Fe3back picture Fe3back · Jun 25, 2017

you can use QValidator it works like:

#To allow only int
self.onlyInt = QIntValidator()
self.LineEdit.setValidator(self.onlyInt)