1

I have this problem with QWebView not showing anything after I rerun QAplication. This small snippet displys the problem:

import sys
from PyQt4 import QtGui, QtWebKit, QtCore

app = QtGui.QApplication(sys.argv)
while True:
    browser = QtWebKit.QWebView()
    browser.setUrl(QtCore.QUrl('https://www.google.ca/#q=pyqt'))
    browser.show()
    app.exec_()

Upon running, the google search page for pyqt is shown, but once I close the widget, next one pops up as blank instead of the same search page. I was wondering what I'm doing wrong here?

1
  • One question would be why you need to call app.exec_() several times. See the answer. Commented Feb 2, 2015 at 9:57

1 Answer 1

1

I do not know why the page stays blank, but I'm certain you can easily achieve the same functionality without calling QApplication.exec_() multiple times.

An example achieving the same:

from PySide import QtGui, QtCore, QtWebKit

class MyBrowser(QtWebKit.QWebView):

    closing = QtCore.Signal()

    def __init__(self):
        super().__init__()

    def closeEvent(self, event):
        self.closing.emit()

class MyApp(QtCore.QObject):

    def __init__(self):
        super().__init__()

    def setup(self):
        self.browser = MyBrowser()
        self.browser.closing.connect(self.setup)
        self.browser.setUrl(QtCore.QUrl('https://www.google.ca/#q=pyqt'))
        self.browser.show()


app = QtGui.QApplication([])
a = MyApp()
a.setup()
app.exec_()
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer! I'm calling app.exec_() in a loop because in the real application I have a call to login window exec_() and then depending on the user I build a QMainWindow with all user-specific UI elements. When the user closes their window, a new login dialog is shown.
@etsinko But do you really need to call app.exec_() more than once or is there another way?
I can probably find the way to avoid calling app.exec_() repeatedly. It will involve changing the current codebase though. I really wonder why consecutive calls to app.exec_() affect webpage rendering.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.