0

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 ... ?

mike rodent
  • 14,126
  • 11
  • 103
  • 157
  • Have you thought about using QDockWidgets, disabling the possibility to dock them? – musicamante Nov 14 '20 at 18:11
  • Thanks. I made that the superclass instead of `QWidget`. It still didn't go away when the main window closed. But maybe I haven't properly understood how `QDockWidget` works... – mike rodent Nov 14 '20 at 18:21
  • Then I'd suggest you to edit the question or, better (since the topic is slightly different), close it and create a new one instead. – musicamante Nov 14 '20 at 18:33
  • Read the documentation about using [dock widgets in QMainWindow](https://doc.qt.io/qt-5/qmainwindow.html#creating-dock-widgets). – musicamante Nov 15 '20 at 00:25

1 Answers1

1

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_())

enter image description here

S. Nick
  • 12,879
  • 8
  • 25
  • 33