小部件的“销毁"信号未触发(PyQT) [英] Widget's "destroyed" signal is not fired (PyQT)
问题描述
我有一个小部件,它在被销毁后必须进行一些手动清理(停止一些线程).但是由于某种原因,小部件的销毁"信号没有触发.我做了这个小例子来演示这个问题.
I have a widget which would have to do some manual cleanup after it's destroyed (stop some threads). However for some reason the "destroyed" signal of the widget is not firing. I have made this small example that demonstrates the problem.
import sys
from PyQt4 import QtGui
class MyWidget(QtGui.QWidget):
def __init__(self, parent):
super(MyWidget, self).__init__(parent)
def doSomeDestruction():
print('Hello World!')
self.destroyed.connect(doSomeDestruction)
class MyWindow(QtGui.QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
self.widget = MyWidget(self)
app = QtGui.QApplication(sys.argv)
window = MyWindow()
window.show()
ret = app.exec_()
sys.exit(ret)
我希望它打印Hello World!"当主窗口关闭时.但是,它不会打印任何内容.
I expected it to print "Hello World!" when the main window is closed. However, it doesn't print anything.
推荐答案
经过几次尝试后,我发现如果您在类外声明 doSomeDestruction
,它 可以工作.(见底部)但我不知道为什么.如这个答案中所写,这是因为在发出destroy()时,小部件不再是QWidget,而只是一个QObject(因为destroy()是从~QObject发出的)
.
这意味着当你的函数被调用时,如果你在类中编写它,它已经被删除了.(另请查看 此处在 qt-interest 邮件列表中:<代码>好的,我很抱歉这个愚蠢的问题.信号发出了,但是由于明显的原因没有调用插槽,那是因为对象已经被删除了.)
After a few tries I found out that it works if you declare the (see at the bottom)doSomeDestruction
outside the class.
But I don't know why. As written in this answer, this is because At the point destroyed() is emitted, the widget isn't a QWidget anymore, just a QObject (as destroyed() is emitted from ~QObject)
.
This means when your function would be called it is already deleted if you write it in the class. (look also here in the qt-interest mailing list: Ok , I am sorry for stupid question. The signal is emitted, but the slot is not called for the obvious reason, that is because object is already deleted.
)
我发现了两种使它真正起作用的方法:
I've found two ways make it really work:
- 在
ret = app.exec_()
之后添加一个简单的del window
. 在主窗口(不是小部件)中设置
WA_DeleteOnClose
属性:
在程序顶部:
- add a simple
del window
afterret = app.exec_()
. Set the
WA_DeleteOnClose
attribute in the main window (not the widget):
At the top of the program:
from PyQt4 import QtCore
改变的__init__
函数:
class MyWindow(QtGui.QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
self.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
self.widget = MyWidget(self)
这篇关于小部件的“销毁"信号未触发(PyQT)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!