Pyside, webkit basic question

2019-03-15 13:39发布

问题:

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_())

回答1:

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_())