如何设置 PyQt5 QIntValidator 的顶部和底部? [英] How to set PyQt5 QIntValidator's top and bottom?
问题描述
我有一个像下面的代码一样的行编辑.在 3 个不同的代码中,我有 2 个不同的问题:
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))
使用这个我可以只输入 0 和 1.
using this I can pnly enter 0 and 1.
和
self.rnr_id_num_le = QLineEdit()
self.rnr_id_num_le.setValidator(QIntValidator(0, 9999999999))
使用这个我只能输入0.
using this I can only enter 0.
我需要它来获取这样的数字:5236147891(位数很重要.如果我不在 QIntValidator 中输入任何数字,它不会让我输入这么大的数字)
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)
基于http://pyqt.sourceforge.net/Docs/PyQt4/qintvalidator.html#QIntValidator-2第二个必须工作;但它没有:(
Based on http://pyqt.sourceforge.net/Docs/PyQt4/qintvalidator.html#QIntValidator-2 the second one must work; but it doesn't :(
好的,如果可以的话,显然它的最高顶部比我需要的少一位数.你知道另一种方法来验证我的 QLineEdit,或者增加 QIntValidator 的顶部吗?
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?
推荐答案
QIntValidator 类仅支持 -2147483648
到 2147483647
范围内的有符号值.如果您需要超出此范围的值,请使用支持无限浮点数的 QDoubleValidator价值.
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.
您可以创建一个简单的 QDoubleValidator
子类来调整行为,使其更像 QIntValidator
:
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)
这篇关于如何设置 PyQt5 QIntValidator 的顶部和底部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!