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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With