Reputation: 827
I want to close all other windows opened by the main window when the main window is closed.
Please find below the min. code that I was testing:
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QLabel, QVBoxLayout, QWidget
import sys
class AnotherWindow(QWidget):
"""
This "window" is a QWidget. If it has no parent, it
will appear as a free-floating window as we want.
"""
def __init__(self):
super().__init__()
layout = QVBoxLayout()
self.label = QLabel("Another Window")
layout.addWidget(self.label)
self.setLayout(layout)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.button = QPushButton("Push for Window")
self.button.clicked.connect(self.show_new_window)
self.setCentralWidget(self.button)
def show_new_window(self, checked):
self.w = AnotherWindow()
self.w.show()
def close_another_window(self):
if self.w:
self.w.close()
app = QApplication(sys.argv)
w = MainWindow()
app.aboutToQuit.connect(w.close_another_window)
w.show()
app.exec()
As shown above I tried using the aboutToQuit
option of the QApplication
, but it only gets called when the another window also is closed.
I want to close the another window automaticaly when the mainwindow is closed.
Upvotes: 9
Views: 11189
Reputation: 1
try my method to close all windows from main window.
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QLabel,
QVBoxLayout, QWidget, QMessageBox
from PyQt5.QtGui import QCloseEvent
import sys
class AnotherWindow(QWidget):
def __init__(self):
super().__init__()
layout = QVBoxLayout()
self.label = QLabel("Another Window")
layout.addWidget(self.label)
self.setLayout(layout)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.button = QPushButton("Push for Window")
self.button.clicked.connect(self.show_new_window)
self.setCentralWidget(self.button)
def show_new_window(self, checked):
self.w = AnotherWindow()
self.w.show()
def close_all_windows(self):
win_list = QApplication.allWindows()
for w in win_list:
w.close()
def closeEvent(self, event: QCloseEvent) -> None:
close = QMessageBox()
close.setText("voulez vous vraiment quitter l'application ?")
close.setStandardButtons(QMessageBox.Yes | QMessageBox.Cancel)
close = close.exec()
if close == QMessageBox.Yes:
event.accept()
self.close_all_windows()
else:
event.ignore()
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec())
Upvotes: 0
Reputation: 31
try this one
def closeEvent(self, event):
QApplication.closeAllWindows()
event.accept()
with QApplication.closeAllWindows() will close every open window
Upvotes: 2
Reputation: 48231
Implement the closeEvent
:
class MainWindow(QMainWindow):
w = None
# ...
def closeEvent(self, event):
if self.w:
self.w.close()
Note that you can also use QApplication.closeAllWindows()
to close any top level window, even without having any direct reference, but if any of those windows ignores the closeEvent()
the function will stop trying to close the remaining.
To avoid that, you can cycle all windows using QApplication.topLevelWidgets()
; windows ignoring the closeEvent
will still keep themselves open, but all the others will be closed:
def closeEvent(self, event):
for window in QApplication.topLevelWidgets():
window.close()
Upvotes: 13
Reputation: 1582
You could try to use signals:
from PyQt5.QtCore import pyqtSignal
class AnotherWindow(QWidget, close_signal):
"""
This "window" is a QWidget. If it has no parent, it
will appear as a free-floating window as we want.
"""
def __init__(self):
super().__init__()
self.close_signal = close_signal
self.close_signal.connect(self.close_me) # connect handler to signal
layout = QVBoxLayout()
self.label = QLabel("Another Window")
layout.addWidget(self.label)
self.setLayout(layout)
def close_me(self):
# handler for signal
self.close()
class MainWindow(QMainWindow):
close_signal = pyqtSignal()
def __init__(self):
super().__init__()
self.button = QPushButton("Push for Window")
self.button.clicked.connect(self.show_new_window)
self.setCentralWidget(self.button)
def show_new_window(self, checked):
self.w = AnotherWindow(self.close_signal)
self.w.show()
def close_another_window(self):
self.close_signal.emit() # fire signal to close other windows
This mechanism allows to close another window even without closing the main window.
(I used signals for other purposes, hope this works as well)
Upvotes: 0