擅长:python、mysql、java
<p>python类实例(或者至少pyqt<;->;qt链接)在发出<code>destroyed</code>时不存在。您可以通过使类上的<code>destroyed</code>处理程序成为<code>staticmethod</code>来解决这个问题。这样,当发出<code>destroyed</code>信号时,python方法仍然存在。</p>
<pre><code>class MyWidget(QWidget):
def __init__(self, parent):
super(MyWidget, self).__init__(parent)
self.destroyed.connect(MyWidget._on_destroyed)
@staticmethod
def _on_destroyed():
# Do stuff here
pass
</code></pre>
<p>如果需要特定于类实例的信息,可以使用<code>functools.partial</code>和实例<code>__dict__</code>将该信息传递给销毁方法。</p>
<pre><code>from functools import partial
class MyWidget(QWidget):
def __init__(self, parent, arg1, arg2):
super(MyWidget, self).__init__(parent)
self.arg1 = arg1
self.arg2 = arg2
self.destroyed.connect(partial(MyWidget._on_destroyed, self.__dict__))
@staticmethod
def _on_destroyed(d):
print d['arg1']
</code></pre>