Do something in mainwindow when a sub window closes
I have a Qmainwindow that has several buttons, which when clicked, open other windows. When one of these sub windows is closed using the red X, I would like to set focus to a particular QLineEdit in the main window, which remains open. I do not want to do any processing in the sub window when it closes, but simply want to do something in the still-open main window. So far:
In one of the sub windows, in the init, I have:
Code:
self.closeEvent(self.mainWindow.reset_focus)
In the mainwindow, I have:
Code:
def reset_focus(self):
self.ui.xyz.setFocus()
self.ui.xyz.selectAll()
I thought I could just set up an event handler in the init of the sub window, but the above does not work. I've read:
https://www.qtcentre.org/threads/610...=close%20event
in this forum, but cannot determine how to translate to my situation, nor can I read the C code.
Re: Do something in mainwindow when a sub window closes
Quote:
self.closeEvent(self.mainWindow.reset_focus)
QWidget::closeEvent() is an event called by the Qt event loop when a widget window is closed. You can't redefine it to do something else.
One solution is to define a custom signal in your child widget class that is emitted when the window receives a close event. Override closeEvent for your child widget, and in it emit the signal.
When you create the child widget in the main window, connect a slot in your main window class to the signal from the child class. When the child window is closed, it will emit the signal, which will call the slot in your main window. Do whatever you want in the slot.