The Monster
The Monster

Reputation: 164

How to set PyQt5 QIntValidator's top and bottom?

I have a line edit like the code below. in 3 different codes I have 2 different problems:

self.rnr_id_num_le = QLineEdit()
self.rnr_id_num_le.setValidator(QIntValidator(9999999999, 0))

using this I can pnly enter 0 and 1.

and

self.rnr_id_num_le = QLineEdit()
self.rnr_id_num_le.setValidator(QIntValidator(0, 9999999999))

using this I can only enter 0.

I need it to get a number like this: 5236147891 ( the number of digits are important. If I don't put any numbers in QIntValidator it won't let me enter a number this big)

Based on http://pyqt.sourceforge.net/Docs/PyQt4/qintvalidator.html#QIntValidator-2 the second one must work; but it doesn't :(

EDIT:

OK, apparently its topest top, if I may, is one digit less than what I need. Do you know another way to validate my QLineEdit, or increase QIntValidator's top?

Upvotes: 3

Views: 4458

Answers (1)

ekhumoro
ekhumoro

Reputation: 120758

The QIntValidator class only supports signed values in the range -2147483648 to 2147483647. If you need values outside this range, use QDoubleValidator, which supports unlimited floating point values.

You can create a simple sub-class of QDoubleValidator to tweak the behaviour so that it's more like QIntValidator:

class BigIntValidator(QtGui.QDoubleValidator):
    def __init__(self, bottom=float('-inf'), top=float('inf')):
        super(BigIntValidator, self).__init__(bottom, top, 0)
        self.setNotation(QtGui.QDoubleValidator.StandardNotation)

    def validate(self, text, pos):
        if text.endswith('.'):
            return QtGui.QValidator.Invalid, text, pos
        return super(BigIntValidator, self).validate(text, pos)

Upvotes: 4

Related Questions