Search code examples
pythonpyqtpyqt4qwidgetqspinbox

PyQt4 QSpinBox value format


My objective is to use a QSpinBox to display the numbers from 0 to 9999 with the increasement of 1 using 4-digits format.

I managed to set the Maximum value 9999 by using setMaximum command. But I can't seems to find a way to display the values in 4digits format (eg. 0000, 0001,0002). Whenever i set the value to 0000 using setValue , the SpinBox display as 0.

How do i display the numbers in 4-digits format (i.e adding leading zero as required) in QSpinBox?


Solution

  • Make a custom QSpinBox, overriding textFromValue:

    class MySpinBox(QtGui.QSpinBox):
        def __init__(self, *args):
           QtGui.QSpinBox.__init__(self, *args)
    
           self.setRange(0,9999)
    
        def textFromValue(self, value):
           return "%04d" % value