无法使用 QWebEngineView.setHtml() 加载 HTML 文件
Cannot load HTML file with QWebEngineView.setHtml()
我的问题的解决方案可能非常简单,但我无法理解。我正在尝试使用 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
我的 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>
奇怪的是(至少对我而言),如果我这样做 self.webView.setHtml("<html><head></head><body><h1>ciao</h1></body></html>")
,这将正确呈现 HTML。
我错过了什么?
setHtml method does exactly what its name suggests: it loads html content from a string. What you are trying to do is load a url, so for that, you need to use the load method:
url = QtCore.QUrl.fromLocalFile(r"C:\DATI\git\webgis\map.html")
self.webView.load(url)
我的问题的解决方案可能非常简单,但我无法理解。我正在尝试使用 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
我的 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>
奇怪的是(至少对我而言),如果我这样做 self.webView.setHtml("<html><head></head><body><h1>ciao</h1></body></html>")
,这将正确呈现 HTML。
我错过了什么?
setHtml method does exactly what its name suggests: it loads html content from a string. What you are trying to do is load a url, so for that, you need to use the load method:
url = QtCore.QUrl.fromLocalFile(r"C:\DATI\git\webgis\map.html")
self.webView.load(url)