如何让 QML 地图流量通过代理,并为程序的其余部分禁用该代理?

How to let QML map traffic pass through a proxy, and disable this proxy for the rest of the program?

我有一个使用本地网络与某些设备通信的应用程序,以及一个用于获取任何外部内容的 http 代理。我想在其中使用地图插件。现在地图需要一个 http 代理来下载瓦片。如果我使用 http_proxy 变量设置全局 Qt 代理设置,或使用 QNetworkProxy::setApplicationProxy 手动设置,则地图加载正常,但程序的其余部分中断。如果我禁用代理,那么我可以与我的设备通话,但我看不到地图。我已尝试使用 QQmlNetworkAccessManagerFactory,如示例 here, and it works fine for an Image type object, as defined in the view.qml 中所述,但如果我将 Image 替换为包含我的地图的 Item,则它不起作用。

Item{
    width: 500
    height: 500
    Map {
        Plugin {
            id: mapPlugin
            name: "osm"
        }
        anchors.fill: parent
        plugin: mapPlugin
        center: QtPositioning.coordinate(59.91, 10.75)
        zoomLevel: 14
        visible: true
    }
}

MyNetworkAccessManagerFactory::create 方法从未被调用,我收到以下错误: QGeoTileRequestManager: Failed to fetch tile (17366,9541,15) 5 times, giving up. Last error message was: 'Error transferring http://c.tile.openstreetmap.org/15/17366/9541.png - server replied: Service Unavailable'。 可能是什么原因造成的?以及如何为地图插件设置 QNetworkProxy?

您必须修改插件的源代码:

src/plugins/geoservices/osm/qgeocodingmanagerengineosm.cpp
src/plugins/geoservices/osm/qgeoroutingmanagerengineosm.cpp
src/plugins/geoservices/osm/qgeotiledmappingmanagerengineosm.cpp
src/plugins/geoservices/osm/qplacemanagerengineosm.cpp

每个文件中都有一个 QNetworkAccessManager,因此您必须添加以下代码:

#include <QtNetwork/QNetworkProxy>

// ...
// constructor

    QString proxy = parameters.value(QStringLiteral("proxy")).toString();
    if (!proxy.isEmpty()) {
#ifndef QT_NO_NETWORKPROXY
        if (proxy.toLower() != QStringLiteral("system")) {
            QUrl proxyUrl(proxy);
            if (proxyUrl.isValid()) {
                qDebug() << "Setting proxy to " << proxyUrl.toString();
                m_networkManager->setProxy(
                            QNetworkProxy(QNetworkProxy::HttpProxy,
                                          proxyUrl.host(),
                                          proxyUrl.port(8080),
                                          proxyUrl.userName(),
                                          proxyUrl.password()));
            }
        } else if (QNetworkProxy::applicationProxy().type() == QNetworkProxy::NoProxy) {
            QNetworkProxyFactory::setUseSystemConfiguration(true);
            qDebug() << "Setting system proxy.";
        }
#else
        qDebug() << "No proxy support";
#endif
    } else {
        qDebug() << "No proxy parameter specified.";
    }

我创建了一个 .patch 来简化更新,因为它从 here 下载。

然后将结尾放入文件夹src/plugins/geoservices/osm,应用补丁,编译并安装如下:

git clone -b 5.12.1 git@github.com:qt/qtlocation.git
cd qtlocation/src/plugins/geoservices/osm
wget https://raw.githubusercontent.com/eyllanesc/Whosebug/master/questions/55105933/proxy-osm.patch
git apply proxy-osm.patch
qmake
make
sudo make install

然后你可以通过插件参数使用代理:

Map {
    Plugin {
        id: mapPlugin
        name: "osm"
        PluginParameter { name: "proxy"; value: "http://179.179.253.147:8080" } // <---
    }
    anchors.fill: parent
    plugin: mapPlugin
    center: QtPositioning.coordinate(59.91, 10.75)
    zoomLevel: 14
    visible: true
}