我需要拦截Spinbox按钮的升压和降压动作以启动一些代码来运行。但是,我相信我必须对抽象类进行子类化,但是我不确定如何正确地进行抽象。
我尚未尝试编写代码,无法通过谷歌搜索找到任何好的示例。我相信spinbox.py文件中的方向正确,但不确定我需要做什么。
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_())
我想拦截升压命令,运行一些代码,然后允许Spinbox执行。我将旋转框用作纯文本编辑字段的翻页程序,随着旋转框增加或减少,它会更改纯文本编辑器上的“页面”。我的代码需要在上一个页面上运行,然后再移动到下一个“页面”。
答案 0 :(得分:1)
您不必:
从QAbstractSpinBox继承,因为使用与QSpinBox继承相同的名称创建一个类不会更改其行为
覆盖stepUp()
方法,因为该方法仅由开发人员调用stepBy()
方法,而您必须覆盖stepBy()
方法并验证步骤是肯定的还是1如您所愿。
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_())
如果要执行main.py中的函数,最好创建一个信号:
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_())