Search code examples
pythonqtwebkitpyside

PySide web browser appears, but inspector doesn't display anything


I am currently running this code, and although the web browser appears, the web inspector doesn't seem to display anything, am i doing something incorrectly?

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

app = QApplication(sys.argv)

web = QWebView()
web.load(QUrl("http://www.google.com"))
web.show()

inspect = QWebInspector()
inspect.setPage(web.page())
inspect.show()

sys.exit(app.exec_())

Solution

  • It is in the Qt Documentation:

    Note: A QWebInspector will display a blank widget if either: page() is null QWebSettings::DeveloperExtrasEnabled is false

    You must enable it, like this:

    import sys
    from PySide.QtCore import *
    from PySide.QtGui import *
    from PySide.QtWebKit import *
    
    app = QApplication(sys.argv)
    
    web = QWebView()
    web.settings().setAttribute(
        QWebSettings.WebAttribute.DeveloperExtrasEnabled, True)
    # or globally:
    # QWebSettings.globalSettings().setAttribute(
    #     QWebSettings.WebAttribute.DeveloperExtrasEnabled, True)
    
    web.load(QUrl("http://www.google.com"))
    web.show()
    
    inspect = QWebInspector()
    inspect.setPage(web.page())
    inspect.show()
    
    sys.exit(app.exec_())