本文介绍了Qt Webengine不加载openstreetmap图块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个这样的python测试程序来显示openstreetmap:

from PyQt5.QtWidgets import QApplication从 PyQt5.QtCore 导入 QUrl从 PyQt5.QtWebEngineWidgets 导入 QWebEngineView导入系统定义 mainPyQt5():url = '文件:///./index.html'app = QApplication(sys.argv)浏览器 = QWebEngineView()browser.load(QUrl(url))浏览器显示()sys.exit(app.exec_())mainPyQt5()

由 QWebEngineView 获取的 index.html 只需调用 openstreetmap:

<link rel = "样式表";href = "http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css"/><div id = 地图"样式 = "宽度:900px;高度:580px"></div><script src = http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script><脚本>//创建地图选项var mapOptions = {中心:[45.641174, 9.114828],变焦:10}//创建地图对象var map = new L.map('map', mapOptions);//创建一个图层对象var layer = new L.TileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png');//添加图层到地图地图.addLayer(层);

如果我使用普通浏览器获取 index.html,地图会按预期显示,但如果我使用 QWebEngineView 调用简单的 python 程序,则不会从 openstreetmap 下载任何图块.如果我用 maps.stamen.com 替换 openstreetmap,无论是浏览器还是 python 脚本,一切都很好.

解决方案

默认情况下,QtWebEngine 不像流行的浏览器那样设置默认标题.在这种情况下,openstreetmap 服务器需要知道Accept-Language" 以过滤非浏览器流量.解决方案是实现一个

I wrote a python test program like this to show openstreetmap:

from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
import sys

def mainPyQt5():
    url = 'file:///./index.html'
    app = QApplication(sys.argv)
    browser = QWebEngineView()
    browser.load(QUrl(url))
    browser.show()
    sys.exit(app.exec_()) 
mainPyQt5()

index.html fetched by QWebEngineView simply calls openstreetmap:

<title>OSM and Leaflet</title>
<link rel = "stylesheet" href = "http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css"/>
<div id = "map" style = "width: 900px; height: 580px"></div><script src =  "http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
<script>
         // Creating map options
         var mapOptions = {
            center: [45.641174, 9.114828],
            zoom: 10
         }
         
         // Creating a map object
         var map = new L.map('map', mapOptions);
         
         // Creating a Layer object
         var layer = new L.TileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png');
         
         // Adding layer to the map
         map.addLayer(layer);
</script>

If I fetch index.html with a ordinary browser the map is shown as expected but if I call the simple python program using QWebEngineView no tiles are downloaded from openstreetmap. If I replace openstreetmap with maps.stamen.com everything is fine both with a browser or the python script.

解决方案

By default QtWebEngine does not set default headers like popular browsers do. In this case the openstreetmap server needs to know the "Accept-Language" to filter non-browser traffic. The solution is to implement a QWebEngineUrlRequestInterceptor that adds that header:

import os.path
import sys

from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineCore import QWebEngineUrlRequestInterceptor
from PyQt5.QtWebEngineWidgets import QWebEngineView


class Interceptor(QWebEngineUrlRequestInterceptor):
    def interceptRequest(self, info):
        info.setHttpHeader(b"Accept-Language", b"en-US,en;q=0.9,es;q=0.8,de;q=0.7")


def mainPyQt5():
    CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
    filename = os.path.join(CURRENT_DIR, "index.html")
    app = QApplication(sys.argv)
    browser = QWebEngineView()

    interceptor = Interceptor()
    browser.page().profile().setUrlRequestInterceptor(interceptor)

    browser.load(QUrl.fromLocalFile(filename))
    browser.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    mainPyQt5()

这篇关于Qt Webengine不加载openstreetmap图块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 00:02