这是我的 PyQt4 的 QDoubleSpinBox 的示例代码。我从该站点获得此代码并对其进行了修改。
class PowerSpinBox(QtGui.QDoubleSpinBox):
def __init__(self, min, max, step, decimals, dec_format, parent=None):
super(PowerSpinBox, self).__init__(parent)
self.setRange(min,max)
self.step = step
self.dec = decimals
self.dec_fmt = '%.6E'
self.setDecimals(decimals)
def textFromValue(self, value):
if value >= 0:
return ' %.6E'%value
else:
return '%.6E'%value
def fixup(self, input):
value = input.toDouble()[0]
print '>>> PowerSpinBox:fixup:', value, input, step, value%step
if (value % self.step) > 0: #self.step*0.1: # old: '> 0:'
if value < 0:
value=value*(-1)
minus=True
else:
minus=False
newValue = value # old: float((int(value*1000)/1)*1)/1000
if minus: newValue = newValue*(-1)
newInput = '%.6E'%newValue # newInput: string
if not minus: newInput = '+'+newInput
print "Invalid value [%s] replaced with [%s]"%(input, newInput)
QtGui.QApplication.beep()
input.replace(0, input.length(), newInput)
def validate(self, input, pos):
valid = QtGui.QValidator.Invalid
newChar = input.at(pos-1)
# print '+++ PowerSpinBox.validate:', input, newChar, pos
if pos < 1:
valid = QtGui.QValidator.Acceptable
else:
if not newChar.isDigit():
if ((newChar.cell() == '.') and (input.count('.') == 1)) or \
((newChar.cell() == '-') and (pos == 1)) or \
((newChar.cell() == '+') and (pos == 1)):
valid = QtGui.QValidator.Acceptable
else:
valid = QtGui.QValidator.Invalid
else:
value = input.toDouble()[0]
if (not (value >= self.minimum() and value <= self.maximum())) or \
(input.length() > input.indexOf('.')+self.decimals()+6):
valid = QtGui.QValidator.Invalid
else:
valid = QtGui.QValidator.Acceptable
return (valid, pos)
# class_PowerSpinBox
在此示例代码中,我想通过 1.0e-14 的步长来控制该值。但我不能那样做。如何在 PyQt4 的 QDoubleSpinBox 中将最小值步长设置为 1.0e-14。
先感谢您。