QAbstractSpinBox是一个抽象类,是将所有步长调节器的通用的功能抽象出了一个父类。虽然QAbstractSpinBox是一个抽象类,但是可以直接实例化使用。QAbstractSpinBox包含了一个QLineEdit和两个QPushbutton。数据的更改可以通过点击按钮或使用键盘输入。

GUI学习之十五——QAbstractSpinBox学习总结

由于QAbstractSpinBox是个基类,没有对按钮的事件进行定义,控件中的按钮点击是没有效果的,想要有效果需要对类进行重写

from PyQt5.Qt import *
import sys
class MyASB(QAbstractSpinBox):
    def stepEnabled(self):
        current = int(self.text())
        if current == 0:
            return QAbstractSpinBox.StepUpEnabled
        elif current == 9999:
            return QAbstractSpinBox.StepDownEnabled
        elif current<0 or current>9999:
            return QAbstractSpinBox.StepNone
        else:
            return QAbstractSpinBox.StepUpEnabled| QAbstractSpinBox.StepDownEnabled

    def stepBy(self, steps: int):
        current =int(self.text())
        self.lineEdit().setText(str(steps+current))
class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.UI_test()

    def UI_test(self):
        asb = MyASB(self)
        asb.move(100,100)
        asb.resize(200,40)
        asb.setAccelerated(True)
        pass
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
QAbstractSpinBox的子类化使用模拟

相关文章: