gpt4 book ai didi

python - PyQt 如何在 closeEvent 中获取发件人(小部件)?

转载 作者:太空宇宙 更新时间:2023-11-03 13:03:19 27 4
gpt4 key购买 nike

我正在尝试为动态添加到 QMainWindow 的几个 dockWidget 捕获一个 closeEvent。我不清楚如何确定哪个小部件已关闭。这是一个简化的示例:

class Example(QtGui.QMainWindow):

def __init__(self):
super(Example, self).__init__()

self.leftDockWidget = QtGui.QDockWidget('pick tool', self)
self.leftDockWidget.setWidget( QtGui.QLabel('a dock widget') )
self.addDockWidget( QtCore.Qt.LeftDockWidgetArea, self.leftDockWidget )
self.leftDockWidget.closeEvent = self.dockWidgetCloseEvent
self.show()

def dockWidgetCloseEvent(self, event):
print event
# how to get sender widget ?

event.sender() 似乎不存在..有什么想法吗?

谢谢

最佳答案

实现您想要的效果的一种方法是使用 event filter :

from PyQt4 import QtGui, QtCore

class Example(QtGui.QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.leftDockWidget = QtGui.QDockWidget('pick tool', self)
self.leftDockWidget.setWidget(QtGui.QLabel('a dock widget'))
self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.leftDockWidget)
self.leftDockWidget.installEventFilter(self)

def eventFilter(self, source, event):
if (event.type() == QtCore.QEvent.Close and
isinstance(source, QtGui.QDockWidget)):
print source.windowTitle()
return super(Example, self).eventFilter(source, event)

if __name__ == '__main__':

import sys
app = QtGui.QApplication(sys.argv)
window = Example()
window.show()
sys.exit(app.exec_())

关于python - PyQt 如何在 closeEvent 中获取发件人(小部件)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12644963/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com