[PyQt] QWebView.page() and setPage()

Jiangcheng Bao jbao605 at gmail.com
Sun Feb 3 23:13:00 GMT 2013


I am trying to store a current page held by QWebView away, then after
loading and processing a different page, restore the previous page,
without having to actually request it again, but it seems that by
doing the following, the application lost my original page, and still
gives me back the new one. Maybe because it is just a reference to the
page itself. Is there anyway I can actually store that page and reuse
it, just like a regular browser would cache the page and load it
relatively quickly when you revisit it?

Thanks
J

import sys
from time import sleep

from PyQt4.QtGui import QApplication
from PyQt4.QtCore import QUrl
from PyQt4.QtWebKit import QWebView

class Browser(QWebView):

    def __init__(self):
        QWebView.__init__(self)
        self.loadFinished.connect(self.load_finished)

    def load_finished(self, ok):
        print('page title: ' + self.title())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    browser = Browser()

    browser.load(QUrl('http://www.google.com'))

    # store this result away
    google_page = browser.page()
    print('current page: ' + browser.title())
    # now load a different page

    browser.load(QUrl('http://www.yahoo.com'))
    print('some other page: ' + browser.title())

    #if browser.page():
    #browser.page().setView(None)

    # lets put back my old page
    browser.setPage(google_page)
    print('my original page: ' + browser.title())

    exit(app.exec_())


More information about the PyQt mailing list