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

Python 无法使用QWebEngineView.setHtml()加载HTML文件,python,qt,pyqt,pyqt5,qwebengineview,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) 我只得到一个字符串,表示HTML文件的路径和名称: C:\DATI\git\webgis\map.html

我的问题的解决方案可能很简单,但我无法理解。我正在尝试使用PyQt5将HTML文件加载到QWebEngineView中。我的做法是:

self.webView = QtWebEngineWidgets.QWebEngineView(self.splitter)
html = r"C:\DATI\git\webgis\map.html"
self.webView.setHtml(html)
我只得到一个字符串,表示HTML文件的路径和名称:

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

My 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>

简单地图
正文{填充:0;边距:0;}
html,body,#map{height:100%;}
奇怪的是(至少对我来说),如果我执行
self.webView.setHtml(“ciao”)
,这将正确地呈现HTML

我遗漏了什么?

的功能完全符合其名称:它从字符串加载html内容。您要做的是加载url,因此,您需要使用:

它完全按照其名称所示:它从字符串加载html内容。您要做的是加载url,因此,您需要使用:

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