【问题标题】:Implement function of a widget in PyQt5 without modify the file generated by pyuic5PyQt5中实现widget的功能,无需修改pyuic5生成的文件
【发布时间】:2020-12-28 19:42:40
【问题描述】:

我想向小部件的方法添加更多功能,但不修改 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_())

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

【问题讨论】:

    标签: python pyqt5


    【解决方案1】:

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

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

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

    1。促销

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

    spinbox.py

    from PyQt5 import QtCore, QtWidgets
    
    
    class SpinBox(QtWidgets.QSpinBox):
        clicked = QtCore.pyqtSignal()
    
        def mousePressEvent(self, event):
            super().mousePressEvent(event)
            self.clicked.emit()
    

    spin.ui

    <?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
    

    ma​​in.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_())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-03
      相关资源
      最近更新 更多