Automatically disconnect after first signal emission

早过忘川 提交于 2019-12-10 17:13:10

问题


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?


回答1:


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 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.



来源:https://stackoverflow.com/questions/15263364/automatically-disconnect-after-first-signal-emission

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!