AQuick
AQuick

Reputation: 176

Subclass Abstract Class

I need to intercept the step-up and step-down actions of the spinbox buttons to initiate some code to run. However, I believe I have to subclass the abstract class, but I'm not entirely certain how to go about doing it correctly.

I haven't tried to code it yet, was unable to find any good examples through googling. I believe have the right direction in the spinbox.py file, but not sure what I need to do exactly.

spinboxui.py

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(200, 150)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox.setObjectName("groupBox")
        self.gridLayout_2 = QtWidgets.QGridLayout(self.groupBox)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.spinBox = QtWidgets.QSpinBox(self.groupBox)
        self.spinBox.setObjectName("spinBox")
        self.gridLayout_2.addWidget(self.spinBox, 0, 0, 1, 1)
        self.label = QtWidgets.QLabel(self.groupBox)
        self.label.setText("")
        self.label.setObjectName("label")
        self.gridLayout_2.addWidget(self.label, 0, 1, 1, 1)
        self.gridLayout.addWidget(self.groupBox, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)

spinbox.py

import sys
from spinboxui import Ui_MainWindow
from PyQt5 import QtCore, QtGui, QtWidgets

class Main(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super().__init__()
        self.setupUi(self)

    def do_something(self):
        pass


class QAbstractSpinBox(QtWidgets.QAbstractSpinBox):

    def __init__(self):
        QAbstractSpinBox.__init__(self)

    def stepUp(self):
        pass


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    spinbox = Main()
    spinbox.show()
    sys.exit(app.exec_())

I'd like to intercept the step up command, run a bit of code, then allow the spinbox to perform. I am using the spinbox as a page turner for a plain text edit field, as the spin box is incremented or decremented, it changes the "page" on the plain text editor. I have code that needs to run on the page it was last on before moving to the next "page".

Upvotes: 0

Views: 132

Answers (1)

eyllanesc
eyllanesc

Reputation: 244252

You do not have to:

  • Inherit from QAbstractSpinBox because creating a class with the same name from which QSpinBox inherits will not change its behavior

  • Overwrite the stepUp() method because that method only calls the stepBy() method by the developer, instead you must overwrite the stepBy() method and verify that the steps are positive or 1 as you wish.

spinbox.py

from PyQt5 import QtWidgets


class SpinBox(QtWidgets.QSpinBox):
    def stepBy(self, steps):
        if steps > 0:
            print("execute function")
        super(SpinBox, self).stepBy(steps)

spinboxui.py

from PyQt5 import QtCore, QtGui, QtWidgets

from spinbox import SpinBox


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(200, 150)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox.setObjectName("groupBox")
        self.gridLayout_2 = QtWidgets.QGridLayout(self.groupBox)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.spinBox = SpinBox(self.groupBox)
        self.spinBox.setObjectName("spinBox")
        self.gridLayout_2.addWidget(self.spinBox, 0, 0, 1, 1)
        self.label = QtWidgets.QLabel(self.groupBox)
        self.label.setText("")
        self.label.setObjectName("label")
        self.gridLayout_2.addWidget(self.label, 0, 1, 1, 1)
        self.gridLayout.addWidget(self.groupBox, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)

main.py

import sys
from spinboxui import Ui_MainWindow
from PyQt5 import QtCore, QtGui, QtWidgets


class Main(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super().__init__()
        self.setupUi(self)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    spinbox = Main()
    spinbox.show()
    sys.exit(app.exec_())

If you want to execute the function in main.py, it is best to create a signal:

spinbox.py

from PyQt5 import QtCore, QtWidgets


class SpinBox(QtWidgets.QSpinBox):
    upSignal = QtCore.pyqtSignal()

    def stepBy(self, steps):
        if steps > 0:
            self.upSignal.emit()
        super(SpinBox, self).stepBy(steps)

main.py

import sys
from spinboxui import Ui_MainWindow
from PyQt5 import QtCore, QtGui, QtWidgets


class Main(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super().__init__()
        self.setupUi(self)
        self.spinBox.upSignal.connect(self.on_up)

    @QtCore.pyqtSlot()
    def on_up(self):
        print("up")


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    spinbox = Main()
    spinbox.show()
    sys.exit(app.exec_())

Upvotes: 1

Related Questions