如何将PyQt4中QDoubleSpinBox的最小值步长设置为1.0E14?

2024-10-01 11:40:05 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的PyQt4的QDoubleSpinBox示例代码。 我从site得到了这段代码并修改了它。你知道吗


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。你知道吗

先谢谢你。你知道吗


Tags: posselfinputifvaluedefstepelse