'PyQt4 spinbox with 64bit integers
is it possible to modify the PyQt spinbox to work with integers bigger than 32bit (-2147483648 - 2147483647)? I know its possible to write a new spinbox in C++ with 64 bit int data type, but this would be to complicate.
Solution 1:[1]
You can get a spin box with values greater than 32bit by using QDoubleSpinBox.
As the name suggests, this will give you double (i.e. floating-point) values with a maximum of 2**53 (9,007,199,254,740,992). If you want integer values, just set the precision to zero by using setDecimals(0). (NB: for some reason, Qt Designer won't allow you to set a maximum value greater than 99,999,999 for either of the built-in spin-boxes, so you'll have to do it "manually").
If you want values greater than 2**53, subclass QAbstractSpinBox, reimplement the relevant methods (stepBy, stepEnabled, validate and possibly fixup), and then provide whatever other methods (e.g. value/setValue) that you deem necessary.
Solution 2:[2]
Many thanks for the hints. I tried the QDoubleSpinBox but didn't tried to set the values manually. Cause it doesn't fit my purposes I've written my own spinbox. Below the code. Maybe its helpful for someone else. Documentation about subclassing QAbstractSpinBox is a bit rare. The default maximum is consistent with numpy.uint64's maximum. In theory there should be no limit, cause an QLineEdit is used for representation. If you need numbers longer than 20 signs simply adapt the validators regular expression.
class BigIntSpinbox(QtGui.QAbstractSpinBox):
def __init__(self, parent=None):
super(BigIntSpinbox, self).__init__(parent)
self._singleStep = 1
self._minimum = -18446744073709551616
self._maximum = 18446744073709551615
self.lineEdit = QtGui.QLineEdit(self)
rx = QtCore.QRegExp("[1-9]\\d{0,20}")
validator = QtGui.QRegExpValidator(rx, self)
self.lineEdit.setValidator(validator)
self.setLineEdit(self.lineEdit)
def value(self):
try:
return int(self.lineEdit.text())
except:
raise
return 0
def setValue(self, value):
if self._valueInRange(value):
self.lineEdit.setText(str(value))
def stepBy(self, steps):
self.setValue(self.value() + steps*self.singleStep())
def stepEnabled(self):
return self.StepUpEnabled | self.StepDownEnabled
def setSingleStep(self, singleStep):
assert isinstance(singleStep, int)
# don't use negative values
self._singleStep = abs(singleStep)
def singleStep(self):
return self._singleStep
def minimum(self):
return self._minimum
def setMinimum(self, minimum):
assert isinstance(minimum, int) or isinstance(minimum, long)
self._minimum = minimum
def maximum(self):
return self._maximum
def setMaximum(self, maximum):
assert isinstance(maximum, int) or isinstance(maximum, long)
self._maximum = maximum
def _valueInRange(self, value):
if value >= self.minimum() and value <= self.maximum():
return True
else:
return False
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | iamantony |
| Solution 2 | user2638109 |
