第一次发射信号后自动断开 [英] Automatically disconnect after first signal emission
问题描述
我正在从文件加载网页,然后替换其中的一些 html:
I am loading a web page from a file, then i replace some of the html in it:
self.template_web_page = QtWebKit.QWebPage()
self.template_web_page.mainFrame().load(QtCore.QUrl('template.html'))
def load(ok):
main_window.web_view.loadFinished.disconnect(load)
self.table_element = self.template_web_page.mainFrame().findFirstElement("#table")
self.table_element.setInnerXml(table_html)
main_window.web_view.loadFinished.connect(load)
有没有办法只为一次发射就连接到信号?
Is there a way to connect to a signal just for one shot?
推荐答案
如前所述,似乎没有比这更好(更简洁)的方法了.
As already noted, there doesn't appear to be a better (more concise) way than this.
http://comments.gmane.org/gmane.comp.lib.qt.general/6883 表明这样的解决方案很好,尽管我自己也遇到了这样的解决方案的问题.我发现如果我在插槽的开头断开插槽的连接(如问题中的代码中所做的那样),然后尝试执行一些 GUI 交互(设置状态栏文本是一个问题,但没有在列表视图中突出显示一行),由于 NULL 指针取消引用,我得到了一个异常.这是针对 Windows 的 Python 2.6 的 PyQt 4.6.2.当我将 disconnect() 调用移到插槽的末尾时,问题就消失了.
http://comments.gmane.org/gmane.comp.lib.qt.general/6883 suggests that such a solution is fine, although I have had issues with such a solution myself. I found that if I disconnected the slot at the start of the slot (as is done in the code in the question) and then tried to perform some GUI interactions (setting statusbar text was a problem, but not highlighting a row in a list view), I got an exception due to a NULL pointer dereference. This was with PyQt 4.6.2 for Python 2.6 for Windows. When I moved the disconnect() call to the end of the slot, the problem went away.
如果这不相关,请提前道歉,这只是我犯的一个愚蠢的错误.
Apologies in advance if this is not relevant and it's just a stupid mistake I've made.
这篇关于第一次发射信号后自动断开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!