首页 > 解决方案 > PyQt5中实现widget的功能,无需修改pyuic5生成的文件

问题描述

我想向小部件的方法添加更多功能,但不修改 pyuic5 生成的文件,因为我正在 QtDesigner 中进行频繁修改,因此文件在不断发展。

我正在考虑将 Main 类中的自定义函数分配给我想要增长的方法,例如:

pyuic5生成的spin.py

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(257, 181)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.spinBox = QtWidgets.QSpinBox(self.centralwidget)
        self.spinBox.setGeometry(QtCore.QRect(102, 60, 42, 22))
        self.spinBox.setObjectName("spinBox")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 257, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))

在此处输入图像描述

自定义 .py 文件 spin_main.py

from PyQt5 import QtWidgets, QtCore, QtGui
from spin import Ui_MainWindow
class MainWindow(Ui_MainWindow, QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.spinBox.mousePressEvent = self.clickEvent

    def clickEvent(self, event):
        print("click event")
    
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    ui = MainWindow()
    ui.show()
    sys.exit(app.exec_())

在这种情况下,我只想添加打印并且该方法继续其功能,但这样我无法保留以前的功能。你给我推荐什么?

标签: pythonpyqt5

解决方案


简单的解决方案是调用基本方法:

def clickEvent(self, event):
    print("click event")
    QtWidgets.QSpinBox.mousePressEvent(self.spinBox, event)

但它不是很优雅,所以我不推荐它,而是有其他选择,例如:

1. 促销

您可以创建自定义 QSpinBox 并通过 QtDesigner 将其包含在 .ui 中(参见12):

旋转盒.py

from PyQt5 import QtCore, QtWidgets


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

    def mousePressEvent(self, event):
        super().mousePressEvent(event)
        self.clicked.emit()

自旋界面

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="SpinBox" name="spinBox">
    <property name="geometry">
     <rect>
      <x>102</x>
      <y>60</y>
      <width>78</width>
      <height>28</height>
     </rect>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>30</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <customwidgets>
  <customwidget>
   <class>SpinBox</class>
   <extends>QSpinBox</extends>
   <header>spinbox.h</header>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>
pyuic5 spin.ui -o spin.py

主文件

from PyQt5 import QtWidgets, QtCore, QtGui
from spin import Ui_MainWindow


class MainWindow(Ui_MainWindow, QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.spinBox.clicked.connect(self.clickEvent)

    def clickEvent(self):
        print("click event")

if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    ui = MainWindow()
    ui.show()
    sys.exit(app.exec_())

2.事件过滤器

由于您想要监听事件,因此您可以实现事件过滤器,因此不再需要覆盖任何方法:

from PyQt5 import QtWidgets, QtCore, QtGui
from spin import Ui_MainWindow


class MainWindow(Ui_MainWindow, QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.spinBox.installEventFilter(self)

    def eventFilter(self, obj, event):
        if obj is self.spinBox and event.type() == QtCore.QEvent.MouseButtonPress:
            self.clickEvent()
        return super().eventFilter(obj, event)

    def clickEvent(self):
        print("click event")


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    ui = MainWindow()
    ui.show()
    sys.exit(app.exec_())

推荐阅读