Connect parent window to child's close event
-
I have a pyside2 mainwindow that creates a child window:
self.newnwindow = QMainWindow() self.window = Ui_MainWindow() self.window.setupUi(self.newwindow) self.newwindow.show()
But I really need to perform an action in the parent when the child window is closed.I mean to connect the parent to it's child close event. Something like this pseudocode(preferible in the parent window):
if(newwindow gets closed): **do something**
I can't figure out how to use the QWidget close and closeEvent functions to report to the MainWindow that the NewWindow has been closed.
-
@jsulm thanks for your answer i tried to do that but i can't figure how to make the override work. For example i have this function in the main window and it is never called, maybe I'm dong something wrong or missing something.
class Ui_MainWindow(QMainWindow): def setupUi(self, MainWindow): ....... def retranslateUi(self, MainWindow): ........ def closeEvent(self,event): print("asdfadf")
The same happens in all the child windows i can't seem to make the overrides work.
-
@pvlopez
I am confused as to what your "main windows" are, you seem to have multiple ones....self.newnwindow = QMainWindow() self.newwindow.show()
This is the child window? Then it will need to be sub-classed from
QMainWindow
, so that you can override its methods likecloseEvent()
.
If you don't want to sub-class you can probably also achieve it by installing aneventFilter()
for the child events in the parent.
Read https://doc.qt.io/qt-6/eventsandfilters.html for all of this. -
-
-
@JonB when you design the interface with QtDesigner it makes a function called setupUI() to create the widgets and a QMainWindow where the centralWidget is going to be placed. If you don't do this the window never appers in the right way:
app = QApplication() mainwindow = QMainWindow() window = Ui_MainWindow() window.setupUi(mainwindow) mainwindow.show() app.exec_()
That's why it seems I have two but one is in top of the other.
-
-
@pvlopez , here is a small example how it works for me.
from PySide6.QtCore import Slot, Signal from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QPushButton, QLabel, QDialog class ChildWnd(QDialog): closed = Signal() def __int__(self): QDialog.__init__(self) @Slot() def closeEvent(self, event): self.closed.emit() super().closeEvent(event) class MainWnd(QMainWindow): def __init__(self): QMainWindow.__init__(self) self.centralwidget = QWidget() self.layout = QVBoxLayout() self.button = QPushButton("Show window") self.button.pressed.connect(self.on_button) self.layout.addWidget(self.button) self.label = QLabel() self.layout.addWidget(self.label) self.centralwidget.setLayout(self.layout) self.setCentralWidget(self.centralwidget) self.child_wnd = None @Slot() def on_button(self): self.child_wnd = ChildWnd(self) self.child_wnd.closed.connect(self.on_child_closed) self.label.setText("") self.child_wnd.show() @Slot() def on_child_closed(self): self.label.setText("Window closed") def main(): app = QApplication() mainwindow = MainWnd() mainwindow.show() app.exec() if __name__ == '__main__': main()
-
@pvlopez said in Connect parent window to child's close event:
@JonB when you design the interface with QtDesigner it makes a function called setupUI() to create the widgets and a QMainWindow where the centralWidget is going to be placed. If you don't do this the window never appers in the right way:
app = QApplication() mainwindow = QMainWindow() window = Ui_MainWindow() window.setupUi(mainwindow) mainwindow.show() app.exec_()
That's why it seems I have two but one is in top of the other.
You need to rework it a bit
class MyMainWindow(QMainWindow): def __init__(self): self.ui = Ui_MainWindow() self.ui.setupUi(self) # Put widgets to this class self.show() # You need closeEvent in a class that is actually shown def closeEvent(self): print('Close event') app = QApplication() window = MyMainWindow() app.exec_()