简体   繁体   中英

Cannot load HTML file with QWebEngineView.setHtml()

The solution of my problem is probably very simple but yet out of my understanding. I am trying to load an HTML file into a QWebEngineView with PyQt5. The way I am doing it is:

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

The only thing I get is a string representing the path and name of my HTML file:

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

My map.html looks like this:

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

Strangely (to me at least), if I do self.webView.setHtml("<html><head></head><body><h1>ciao</h1></body></html>") , this will render the HTML properly.

What am I missing?

The 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM