简体   繁体   中英

How to restrict user input in QLineEdit in pyqt

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.

you can use QValidator it works like:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM