Dodrekai
Dodrekai

Reputation: 13

Override QSpinBox.stepby method make spinbox only increment

I want to override the stepBy method of QSpinBox in pyqt5 in order to force verifications before the value is changed. But with my code when I click on the "increase" button of the spinbox, all goes well, but the "decrease" button doesn't seem to work... Why only this one ?

Here is the code :

class custom_spinbox(PyQt5.QtWidgets.QSpinBox):
    def __init__(self, *args, **kwargs):
        super(custom_spinbox, self).__init__(*args, **kwargs)

    def stepBy(self, step):
        if step == 1:
            print("up")
        if step == -1:
            print("down")

class ui_attr(QWidget):
    def __init__(self, *args, **kwargs):
        super(ui_attr, self).__init__()
        #loading all the stuff
        uic.loadUi("wdg_attr_2.ui", self)

        test = custom_spinbox()
        self.grid.addWidget(test, 1, 1)

    def aff(self):
        self.show()

Increment button print "up" on the console but decrement button doesn't show anything. I tried subclassing QAbstractSpinBox instead of QSpinBox, but got nothing printed on the console at all.

What did I miss ?

Upvotes: 1

Views: 1055

Answers (1)

Marcus
Marcus

Reputation: 1703

You are overriding stepBy, and that mean it's up to you to change the value shown in the spinbox. In your code, the value does not change at all.

Now, by default, the minimum value of the spinbox is 0. And at zero, the step down button is disabled. Hence, your clicking of step-down button does not produce any output.

Just add self.setMinimum( -100 ) after super(custom_spinbox, ... and you'll see that 'down' will be printed.

As for the subclassing of QAbstractSpinBox, you'll have to do a hell lot of work to make it behave like QSpinBox, it's not worth the effort for so trivial a feature as you want.

Upvotes: 1

Related Questions