Automatically disconnect after first signal emissi

2019-06-24 07:51发布

问题:

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.



标签: qt qt4 pyqt pyqt4