使用线程时没有调用QWebPage loadFinished信号?

时间:2011-12-27 11:18:31

标签: pyqt4 qtwebkit qthread

我正在尝试在辅助线程中使用webkit执行网络请求,并且还执行所有处理,例如在同一线程中报废数据。但是,我很困惑为什么QWebPage的信号loadFinished没有连接。在我还在学习的时候请赐教。

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

class HeadlessBrowser(QObject):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.page = QWebPage()

    def browse_page(self, url):
        qDebug('method browse_page called.') #This is called.
        url = QUrl(url)
        self.page.mainFrame().load(url)
        self.page.loadFinished.connect(self.process_page) #Not executed. Why?

    def process_page(self):
        qDebug('method process_page called.') #This is not called. Why?
        #Convert to html
        print(self.page.mainFrame().toHtml())

class WorkerThread(QThread):
    def __init__(self, parent=None):
        super().__init__(parent)

    def run(self):

        br = HeadlessBrowser()
        br.browse_page('http://www.google.com')

    def __del__(self):
        self.wait()

def main():
    app = QApplication(sys.argv)
    thread = WorkerThread()
    thread.start()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

0 个答案:

没有答案