我有两个打开的主窗口:带按钮的主窗口和带停靠的主窗口。后者包含一个QDockWidget。在
IS行为:当用户使DockWidget可浮动并用dock关闭主窗口时,DockWidget不会关闭。在
应该行为:当用户使DockWidget可浮动并使用Dock关闭主窗口时,DockWidget也将关闭。在
注意事项:
示例代码:
from PyQt4 import QtCore, QtGui
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QApplication, QDialog, QMainWindow
import sys
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindowWithButton(object):
def setupUi(self, MainWindowWithButton):
MainWindowWithButton.setObjectName(_fromUtf8("MainWindowWithButton"))
MainWindowWithButton.resize(567, 384)
self.centralwidget = QtGui.QWidget(MainWindowWithButton)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
MainWindowWithButton.setCentralWidget(self.centralwidget)
def retranslateUi(self, MainWindowWithButton):
MainWindowWithButton.setWindowTitle(_translate("MainWindowWithButton", "MainWindow", None))
class Ui_MainWindowWithDock(object):
def setupUi(self, MainWindowWithDock):
MainWindowWithDock.setObjectName(_fromUtf8("MainWindowWithDock"))
MainWindowWithDock.resize(509, 316)
self.centralwidget = QtGui.QWidget(MainWindowWithDock)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
MainWindowWithDock.setCentralWidget(self.centralwidget)
# # # # # # # # # # # # # # # # # # # # # #
# # # setup dock # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # #
self.theDock = QtGui.QDockWidget(MainWindowWithDock)
self.theDock.setObjectName(_fromUtf8("theDock"))
self.dockWidgetContents = QtGui.QWidget(self.theDock)
self.dockWidgetContents.setObjectName(_fromUtf8("dockWidgetContents"))
self.theDock.setWidget(self.dockWidgetContents)
MainWindowWithDock.addDockWidget(QtCore.Qt.DockWidgetArea(2), self.theDock)
self.retranslateUi(MainWindowWithDock)
QtCore.QMetaObject.connectSlotsByName(MainWindowWithDock)
def retranslateUi(self, MainWindowWithDock):
MainWindowWithDock.setWindowTitle(_translate("MainWindowWithDock", "MainWindow", None))
class MainWindowWithButtonDlg(QMainWindow):
pass
class MainWindowWithDockDlg(QMainWindow):
pass
def main():
app = QApplication(sys.argv)
windowWithDockUi = Ui_MainWindowWithDock()
windowWithDock = MainWindowWithDockDlg()
windowWithDockUi.setupUi(windowWithDock)
windowWithDock.show()
app.exec()
# the dock widget should be closed by now
ui = Ui_MainWindowWithButton()
window = MainWindowWithButtonDlg()
ui.setupUi(window)
window.show()
app.exec()
if __name__ == '__main__':
main()
原始源的拒绝方法。这里我们有一个QDialog,QMainwindow是它的中心小部件,因此在某种意义上它变成了QMainwindow(from Anki addCards.py (scroll to bottom)):
^{pr2}$
可以使用event-filter监视给定窗口的所有事件。就在窗口关闭之前,它将始终发布关闭事件。窗口是否修改了正常的关闭过程并不重要。如果并且当它最终关闭时,相应关闭事件的accept property被保证为
True
。所以,如果你关注这个事件,你可以简单地检查一下它是否被接受,然后相应地采取行动。在要解决的主要问题是如何找到合适的窗口观看。在创建dock小部件时,这可能无法访问。因此,一种方法是等到dock小部件的父窗口首次显示出来,然后查找顶层窗口并在其上安装事件过滤器。这样做意味着dock小部件永远不需要知道它最终依赖的窗口的任何信息。在
以下是基于您的示例的这种方法的工作演示(删除了大部分不相关的内容):
相关问题 更多 >
编程相关推荐