[英]QWebView doesn't load any external resources if it loads a html-file from qresources
[英]PyQt5 QWebView: Load .html file that loads .js file
我試圖顯示QWebView
的傳單地圖(從此處啟發)。 我的文件夾的結構如下:
webkit_leaflet/
├── map.html
├── map.js
└── map.py
當我運行map.py
並包含map.html
和map.js
所有內容時,代碼就會起作用。
from PyQt5 import QtWidgets, QtWebKitWidgets
import sys
# Create application
app = QtWidgets.QApplication(sys.argv)
# Add window
win = QtWidgets.QWidget()
win.setWindowTitle('QWebView Map Test')
# Add layout
layout = QtWidgets.QVBoxLayout()
win.setLayout(layout)
# Create QWebView
view = QtWebKitWidgets.QWebView()
# include code from map.html and map.js
view.setHtml('''
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
<style>
body { padding: 0; margin: 0; }
html, body, #map { height: 100%; }
</style>
</head>
<body>
<div id="map"></div>
<script>
var map = L.map('map').setView([42.35, -71.08], 13);
L.tileLayer('http://tiles.mapc.org/basemap/{z}/{x}/{y}.png',
{
maxZoom: 18,
attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
'<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
'Imagery © <a href="http://mapbox.com">Mapbox</a>',
id: 'examples.map-i86nkdio',
}).addTo(map);
</script>
</body>
</html>
''')
# Add QWebView to the layout
layout.addWidget(view)
# Show window, run app
win.show()
app.exec_()
但是,如果我嘗試使用QtCore.QUrl()
加載map.html
, QtCore.QUrl()
不會發生任何事情。
from PyQt5 import QtCore, QtWidgets, QtWebKitWidgets
import sys
# Create application
app = QtWidgets.QApplication(sys.argv)
# Add window
win = QtWidgets.QWidget()
win.setWindowTitle('QWebView Map Test')
# Add layout
layout = QtWidgets.QVBoxLayout()
win.setLayout(layout)
# Create QWebView
view = QtWebKitWidgets.QWebView()
# load .html file
view.load(QtCore.QUrl('map.html'))
layout.addWidget(view)
win.show()
app.exec_()
當我從外部.html文件中加載JavaScript文件時,有人可以告訴我如何在PyQt5中顯示.html文件的內容嗎?
這是map.html
的代碼:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
<style>
body { padding: 0; margin: 0; }
html, body, #map { height: 100%; }
</style>
</head>
<body>
<div id="map"></div>
<script src="map.js"></script>
</body>
</html>
還有map.js
的代碼
var map = L.map('map').setView([42.35, -71.08], 13);
L.tileLayer('http://tiles.mapc.org/basemap/{z}/{x}/{y}.png',
{
maxZoom: 18,
attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
'<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
'Imagery © <a href="http://mapbox.com">Mapbox</a>',
id: 'examples.map-i86nkdio',
}).addTo(map);
使用QUrl.fromLocalFile傳遞html文件。 似乎您還需要將絕對路徑傳遞到文件。
import os
view.load(QtCore.QUrl.fromLocalFile(os.path.abspath('map.html')))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.