创建依赖window?
Create a dependent window?
我想创建一个相对于主要 window 自由浮动的辅助 window。但我也希望它在主 window 关闭时关闭。事实上,我希望应用程序在主 window 关闭时完全结束,并假设这是默认行为。
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())
如代码所示,关闭主要 window 不会关闭次要 window。
我认为可能使“主”window 成为次要 window 的父级可能会产生这种开箱即用的行为,但似乎不会:相反,次要 window 然后“合并”到主要 window.
这是完成特定任务的一种方法:
class MainWindow( QMainWindow ):
...
def closeEvent( self, event ):
super().closeEvent( event )
QApplication.instance().exit()
...但这本身并不能解决如何使一个 window 依赖于另一个的问题。
也许您必须明确地对其进行硬编码? IE。密切关注任何辅助 windows(例如在 list
中),然后在“主人”window 经历 closeEvent
时明确关闭它们 ... ?
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_())
我想创建一个相对于主要 window 自由浮动的辅助 window。但我也希望它在主 window 关闭时关闭。事实上,我希望应用程序在主 window 关闭时完全结束,并假设这是默认行为。
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())
如代码所示,关闭主要 window 不会关闭次要 window。
我认为可能使“主”window 成为次要 window 的父级可能会产生这种开箱即用的行为,但似乎不会:相反,次要 window 然后“合并”到主要 window.
这是完成特定任务的一种方法:
class MainWindow( QMainWindow ):
...
def closeEvent( self, event ):
super().closeEvent( event )
QApplication.instance().exit()
...但这本身并不能解决如何使一个 window 依赖于另一个的问题。
也许您必须明确地对其进行硬编码? IE。密切关注任何辅助 windows(例如在 list
中),然后在“主人”window 经历 closeEvent
时明确关闭它们 ... ?
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_())