I have a spinbox in PyQt5 like below. How can I make the selection reversed? i.e. If I click down arrow, the value goes up, vice versa.
Advertisement
Answer
A possible solution is to override the stepBy()
and stepEnabled()
methods:
JavaScript
x
26
26
1
import sys
2
from PyQt5 import QtWidgets
3
4
5
class ReverseSpinBox(QtWidgets.QSpinBox):
6
def stepEnabled(self):
7
if self.wrapping() or self.isReadOnly():
8
return super().stepEnabled()
9
ret = QtWidgets.QAbstractSpinBox.StepNone
10
if self.value() > self.minimum():
11
ret |= QtWidgets.QAbstractSpinBox.StepUpEnabled
12
if self.value() < self.maximum():
13
ret |= QtWidgets.QAbstractSpinBox.StepDownEnabled
14
return ret
15
16
def stepBy(self, steps):
17
return super().stepBy(-steps)
18
19
20
if __name__ == "__main__":
21
app = QtWidgets.QApplication(sys.argv)
22
w = ReverseSpinBox()
23
w.resize(320, 20)
24
w.show()
25
sys.exit(app.exec_())
26