1

I'm having a problem with the closeEvent() function override in a class which is not triggering. This is the code I have in the main.py file:

from mainwindow import *

if __name__ == '__main__':
    app = QApplication()
    mainwindow = QMainWindow()
    window = Ui_MainWindow()
    window.setupUi(mainwindow)
    mainwindow.show()
    app.exec_()

And then in another file named mainwindow.py I have:

class Ui_MainWindow(QMainWindow):
    def setupUi(self, MainWindow):
      .......
    def retranslateUi(self, MainWindow):
         ........
    def closeEvent(self,event):
        print("Testing")

But when I close the window the closeEvent() function never runs. I'm missing something or doing it wrong but I can't figure out what it is. Please help.

PedroVLP
  • 15
  • 5
  • 1
    Does this answer your question? [QtDesigner changes will be lost after redesign User Interface](https://stackoverflow.com/questions/46544780/qtdesigner-changes-will-be-lost-after-redesign-user-interface) – musicamante Feb 17 '23 at 15:38
  • No it doesn't because I used QtDesigner to just to create the interface and produce the respective .py files but i'm not pretending to use it anymore because the interface is already done.Thanks anyway. – PedroVLP Feb 17 '23 at 15:42
  • 1
    That was an automated comment StackOverflow creates after a close vote. And, it *does* answer your question because you're *not* using the pyuic generated class properly: those files should **never** be modified, only imported and used as composition or multiple inheritance in the *actual* main files. Trying to change their behavior is a common mistake that leads to unexpected results, just like yours: you're not showing your `Ui_MainWindow` instance, you are showing a QMainWindow one (which uses components created in `Ui_MainWindow.setupUi()`), so that `closeEvent` will never be triggered. – musicamante Feb 17 '23 at 15:58
  • 1
    Regenerate those files, leave them **unmodified**, then create a *new* script which will be the actual program, import that class, and use it as base class along with QMainWindow: `class MainWindow(QMainWindow, Ui_MainWindow):` then implement the `__init__` by calling use `super().__init__()` and `self.setupUi(self)`, finally add the `closeEvent()` override. You can see some examples in the PyQt guidelines about [using Designer](//www.riverbankcomputing.com/static/Docs/PyQt5/designer.html) (it works the same for PySide, except for the obvious different import statements). – musicamante Feb 17 '23 at 16:02
  • I think that would be a good idea I'll try that approach and report the results. – PedroVLP Feb 17 '23 at 16:06
  • That's exactly what the answers to that post explain, btw. Note that while you *may* think that you can safely edit pyuic files because you won't change the UI anymore, you're likely to regret that choice: first of all, you may realize that you want to change something in the UI, which would be very easy to do in Designer, but since you've modified the python file, merging those changes is often difficult (and you may overwrite your current file by mistake); then, changing the behavior/inheritance of those base classes is pointless and usually wrong, unless you *really* know what you're doing. – musicamante Feb 17 '23 at 16:09
  • I did exactly that: class Dupy(QMainWindow, Ui_MainWindow): def __init__(): super.__init__(self, *args, **kwargs) self.setupUi(self) But I'm having this error now: class Dupy(QMainWindow, Ui_MainWindow): TypeError: Cannot create a consistent method resolution order (MRO) for bases QMainWindow, Ui_MainWindow – PedroVLP Feb 17 '23 at 16:20
  • Did you regenerate the Ui_MainWindow from the UI file? – musicamante Feb 17 '23 at 16:21
  • Please use \``backticks`\`, otherwise the code is impossible to read. – musicamante Feb 17 '23 at 16:47
  • I did regenerate the interfaces but now i have another error: `class Dupy(QMainWindow, Ui_MainWindow): def __init__(self): super.__init__() self.setupUi(self)` This is the error: `TypeError: descriptor '__init__' of 'super' object needs an argument` – PedroVLP Feb 17 '23 at 16:55
  • `super().__init__()`. You missed the parentheses. Let me be frank, the next time you think you could complain about a language (as you did the other day), at least ensure that you really know how it works and how to use it; because if you did, you wouldn't have made such a *basic* mistake. – musicamante Feb 17 '23 at 17:12

0 Answers0