[PyQt] QDoubleSpinBox & it's precision

Hans-Peter Jansen hpj at urpla.net
Fri Oct 9 19:55:00 BST 2009


Am Freitag 09 Oktober 2009 schrieb Prashant Saxena:
> Here is the script.
>
> #!/usr/bin/env python
>
> from PyQt4 import QtCore, QtGui
>
> __SINGLESTEP__ = 0.001
> __DECIMALS__ = 3
> __PRECISION__ = 0.001
> __HSPACING__ = 5
> __LABELWIDTH__ = 100
> __2ndWIDGET_WIDTH__ = 70
>
> class QxDoubleSpinBox(QtGui.QDoubleSpinBox):
>     def __init__(self, parent=None, value=0.):
>         QtGui.QDoubleSpinBox.__init__(self, parent)
>         self.setRange(0., 1.)
>         self.setSingleStep(__SINGLESTEP__)
>         self.setFixedWidth(__2ndWIDGET_WIDTH__ )
>         self.setWrapping(True)
>         self.setDecimals(__DECIMALS__)

Okay, obviously calling these member functions had side effects on the value 
itself (as noted in the docs). They also trigger the call to textFromValue.
Just move setValue here, and you're fine.

         self.setValue(0.421)

>     def valueFromText(self, str):
>         return FixedPoint(str, 3)
>
>     def textFromValue(self, value):
>         print "textFromValue = ", value
>         #return str(FixedPoint(value, 3)) # 'FixedPoint' Custom Floating
> Point Management class return str(value)
>
> class Window(QtGui.QWidget):
>     def __init__(self):
>         super(Window, self).__init__()
>
>         self.createDoubleSpinBoxes()
>         layout = QtGui.QHBoxLayout()
>         layout.addWidget(self.doubleSpinBoxesGroup)
>         self.setLayout(layout)
>         self.setWindowTitle("Spin Boxes")
>
>
>     def createDoubleSpinBoxes(self):
>         self.doubleSpinBoxesGroup = QtGui.QGroupBox("Double precision
> spinboxes") self.doubleSpinBox = QxDoubleSpinBox(self, 0.421)
>
>         spinBoxLayout = QtGui.QVBoxLayout()
>         spinBoxLayout.addWidget(self.doubleSpinBox)
>         self.doubleSpinBoxesGroup.setLayout(spinBoxLayout)
>
>
> if __name__ == '__main__':
>
>     import sys
>     app = QtGui.QApplication(sys.argv)
>     window = Window()
>     window.show()
>     sys.exit(app.exec_())
>
>
>
> Prashant
>
> qt-sdk-win-opensource-2009.03.1.exe
> Python 2.6.3
> PyQt-Py2.6-gpl-4.6-1
> Win XP, 32 Bit
>

See, how much nicer your message appear on the ML, and how easy it could 
processed..

Pete


More information about the PyQt mailing list