Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restrict user input in QLineEdit in pyqt

Tags:

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.

like image 660
Rao Avatar asked Apr 05 '13 08:04

Rao


2 Answers

you can use QValidator it works like:

#To allow only int self.onlyInt = QIntValidator() self.LineEdit.setValidator(self.onlyInt) 
like image 86
Fe3back Avatar answered Sep 25 '22 13:09

Fe3back


you can use exception handling for validating this:

number = self.ui.number_lineEdit.text() try:     number = int(number) except Exception:     QtGui.QMessageBox.about(self, 'Error','Input can only be a number')     pass 

you can also use validators to validate input strings.

like image 22
scottydelta Avatar answered Sep 24 '22 13:09

scottydelta