无法使用QWebEngineView.setHtml()加载HTML文件

umb*_*987 0 python qt pyqt pyqt5 qwebengineview

我的问题的解决方法可能很简单,但仍然超出我的理解范围。我正在尝试使用PyQt5将HTML文件加载到QWebEngineView中。我这样做的方式是:

self.webView = QtWebEngineWidgets.QWebEngineView(self.splitter)
html = r"C:\DATI\git\webgis\map.html"
self.webView.setHtml(html)
Run Code Online (Sandbox Code Playgroud)

我唯一得到的是一个字符串,代表我的HTML文件的路径和名称:

C:\ DATI \ git \ webgis \ map.html

我的map.html看起来像这样:

<html>
  <head>
    <title>Simple Map</title>
    <link rel="stylesheet" href="https://openlayers.org/en/v4.5.0/css/ol.css" type="text/css">
    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
    <script src="https://openlayers.org/en/v4.5.0/build/ol.js"></script>
    <script src=".js/qwebchannel.js"></script>
    <style>
        body { padding: 0; margin: 0; }
        html, body, #map { height: 100%; }
    </style>
  </head>
  <body>
    <div id="map" class="map"></div>
    <script  src="./js/map.js"></script>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

奇怪的是(至少对我来说),如果这样做的话self.webView.setHtml("<html><head></head><body><h1>ciao</h1></body></html>"),它将正确地呈现HTML。

我想念什么?

ekh*_*oro 5

setHtml方法完全按照其名称的含义进行操作:它从字符串加载html内容。您要尝试的是加载url,因此,您需要使用load方法

url = QtCore.QUrl.fromLocalFile(r"C:\DATI\git\webgis\map.html")
self.webView.load(url)
Run Code Online (Sandbox Code Playgroud)