سؤال

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?

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

To do what you want, just set the "prefix" attribute of the spinbox widget to "000". This will then pad values to be 0001, 0002, etc.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top