I want to create a secondary window which is free-floating in relation to the main window. But I also want it to close when the main window closes. Indeed, I want the application to end completely when the main window closes, and assumed this would be default behaviour.
MRE:
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QLabel, QVBoxLayout import sys class MainWindow( QMainWindow ): def __init__( self ): super().__init__() self.secondary_window = SecondaryWindow() # self.secondary_window = SecondaryWindow( self ) class SecondaryWindow( QWidget ): def __init__( self, *args ): super().__init__( *args ) layout = QVBoxLayout() self.label = QLabel( 'Another Window' ) layout.addWidget(self.label ) self.setLayout(layout) app = QApplication([]) application = MainWindow() application.move( 500, 500 ) application.show() application.secondary_window.show() sys.exit(app.exec())
As the code is, closing the main window does not close the secondary window.
I thought possibly that making the “master” window the parent of the secondary window might produce this behaviour out of the box, but it appears not: instead the secondary window then gets “incorporated” into the main window.
This is one way to accomplish the specific task:
class MainWindow( QMainWindow ): ... def closeEvent( self, event ): super().closeEvent( event ) QApplication.instance().exit()
… but this doesn’t in itself provide a solution to the question of how to make one window dependent on another.
Maybe you have to hard-code it explicitly? I.e. keep tabs on any secondary windows (e.g. in a list
) and then explicitly close them when the “master” window experiences closeEvent
… ?
Advertisement
Answer
flags Qt::WindowFlags
This enum type is used to specify various window-system properties for the widget. They are fairly unusual but necessary in a few cases. Some of these flags depend on whether the underlying window manager supports them.
Qt::Window
Indicates that the widget is a window, usually with a window system frame and a title bar, irrespective of whether the widget has a parent or not. Note that it is not possible to unset this flag if the widget does not have a parent.
https://doc.qt.io/qt-5/qt.html#WindowType-enum
import sys from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QLabel, QVBoxLayout from PyQt5.QtCore import Qt class SecondaryWindow( QWidget ): def __init__( self, parent): super(SecondaryWindow, self).__init__(parent, Qt.Window) # <<<=== Qt.Window self.setWindowTitle("SecondaryWindow") layout = QVBoxLayout() self.label = QLabel( 'Another Window' ) layout.addWidget(self.label ) self.setLayout(layout) class MainWindow( QMainWindow ): def __init__( self ): super().__init__() self.setWindowTitle("MainWindow") # self.secondary_window = SecondaryWindow() self.secondary_window = SecondaryWindow( self ) # self self.secondary_window.show() app = QApplication([]) application = MainWindow() application.move( 500, 500 ) application.show() #application.secondary_window.show() sys.exit(app.exec_())