簡體   English   中英

無法使用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方法完全按照其名稱的含義進行操作:它從字符串加載html內容 您要嘗試的是加載url ,因此,您需要使用load方法

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM