QtWebkit:从不执行loadFinished和loadProgress插槽

时间:2011-04-23 15:30:00

标签: python qt pyqt qtwebkit

这是我试用的Webkit的基本PyQ​​t代码。

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
from PyQt4.QtNetwork import *

class XPrinter(QObject):

    def __init__(self):
        QObject.__init__(self)

    def print_page_info(self, ok):
        print ok

    def print_load_started(self):
        print 'started loading'

    def print_load_percent(self, percent):
        print percent


app = QApplication(sys.argv)

web = QWebView()
xprinter = XPrinter()
QObject.connect(web, SIGNAL("loadFinished()"), xprinter.print_page_info)
QObject.connect(web, SIGNAL("loadStarted()"), xprinter.print_load_started)
QObject.connect(web, SIGNAL("loadProgress()"), xprinter.print_load_percent)
web.load(QUrl("http://www.gnu.org"))
web.setWindowState(Qt.WindowMaximized)
web.show()

sys.exit(app.exec_())

我遇到的问题是插槽loadFinished和loadProgress永远不会被执行。请告诉我我做错了什么?

1 个答案:

答案 0 :(得分:4)

尝试使用新的style signals

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
from PyQt4.QtNetwork import *

class XPrinter(QObject):

    def __init__(self):
        QObject.__init__(self)

    def print_page_info(self, ok):
        print ok

    def print_load_started(self):
        print 'started loading'

    def print_load_percent(self, percent):
        print percent


app = QApplication(sys.argv)

web = QWebView()
xprinter = XPrinter()
web.loadFinished.connect(xprinter.print_page_info)
web.loadStarted.connect(xprinter.print_load_started)
web.loadProgress.connect(xprinter.print_load_percent)

web.load(QUrl("http://www.gnu.org"))
web.setWindowState(Qt.WindowMaximized)
web.show()

sys.exit(app.exec_())

修改 你也有错误的签名 QWebview

QObject.connect(web, SIGNAL("loadFinished(bool)"), xprinter.print_page_info)
QObject.connect(web, SIGNAL("loadStarted()"), xprinter.print_load_started)
QObject.connect(web, SIGNAL("loadProgress(int )"), xprinter.print_load_percent)