简体   繁体   中英

PyQt QtWebKit loadFinished not called

I have this script with which I would like to do some more stuff when it is ready:

from PyQt4 import QtCore, QtGui, QtWebKit

class WebViewCreator:
    def __init__(self):

        self.view = QtWebKit.QWebView()
        self.view.setPage(QtWebKit.QWebPage())

        self.view.connect(self.view, QtCore.SIGNAL('loadFinished(bool)'), self.load_finished)

        path = self.app.resources_uri() + "/index.html"
        self.view.load(QtCore.QUrl(path))

    def load_finished(self, ok):
        print ok

def onDone(ok):
    print ok

The problem I have is that if I connect a function to the loadFinished(bool) signal then the function gets executed, but if I connect a method of the object like self.load_finished then this method is not called and I can't understand why :-/

The same happens with:

self.view.loadFinished.connect(onDone)

versus:

self.view.loadFinished.connect(self.load_finished)

There's nothing obviously wrong with the code you posted.

Which is to say, when I run this slightly simplified version of it, it loads the page successfully and (eventually) prints True :

from PyQt4 import QtCore, QtGui, QtWebKit

class WebViewCreator(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.view = QtWebKit.QWebView(self)
        self.view.loadFinished.connect(self.load_finished)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.view)
        self.view.load(QtCore.QUrl('http://stackoverflow.com/'))

    def load_finished(self, ok):
        print ok

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = WebViewCreator()
    window.show()
    sys.exit(app.exec_())

This is using Python 2.7.3, Qt 4.8.3, and PyQt 4.9.5 on Linux.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM