1

I want to add more functionality to a method of a widget but without modify the file generated by pyuic5 because I'm doing modifies in the QtDesigner with frequency so the file is on constant evolution.

I'm thinking in assign a custom function in the Main class to a method that I want grown, for example:

spin.py generated by pyuic5

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"))

enter image description here

custom .py file 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_())

In this case I want add only the print and that the method continues with its function but with this way I'm cannot keep the previous functionality. What do you recommend me?

Caeta
  • 431
  • 3
  • 14

1 Answers1

2

The trivial solution is to call the base method:

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

But it is not very elegant so I do not recommend it, instead there are other options such as:

1. Promotion

You can create a custom QSpinBox and include it in the .ui through QtDesigner(See 1 and 2):

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

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.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. EventFilter

Since what you want is to listen to an event then you can implement an event filter so it is no longer necessary to override any method:

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_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241