简体   繁体   English

如何在Qt中设置应用程序范围的代理设置?

[英]How can I set application wide proxy settings in Qt?

I have posted previously about my efforts to get Google Maps to display in WebView. 我之前发布过有关将Google地图显示在WebView中的努力。 I discovered that I needed to install OpenSSL so that HTTPS could be used for some of the Google APIs. 我发现我需要安装OpenSSL,以便HTTPS可以用于某些Google API。

That's all well and good but now on a different machine I am having touble configuring Qt to use the system proxy. 这一切都很好,但现在在不同的机器上,我正在配置Qt以使用系统代理。 For this one we have one system proxy that does not require authentication and one that does. 对于这个,我们有一个不需要身份验证的系统代理,也有一个。 The default proxy is the one that does require authentication. 默认代理是需要身份验证的代理。

I have the following code to specify the proxy: 我有以下代码来指定代理:

#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include <QNetworkProxy>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QNetworkProxy proxy;
     proxy.setType(QNetworkProxy::HttpProxy);
     proxy.setHostName("url.of.proxy.not.requiring.authentication");
     proxy.setPort(8080);
     QNetworkProxy::setApplicationProxy(proxy);

    QtQuick2ApplicationViewer viewer;
    viewer.setMainQmlFile(QStringLiteral("qml/Maps/main.qml"));
    viewer.showExpanded();

    return app.exec();
}

When I run this application I get a web page displayed in WebView whose contents state there is an error 407 because the proxy requires authentication and the proxy address specified in this page is the default one and not the one I am explicitly telling Qt to use. 当我运行这个应用程序时,我得到一个显示在WebView中的网页,其内容状态存在错误407,因为代理需要身份验证,并且此页面中指定的代理地址是默认的,而不是我明确告诉Qt使用的代理地址。

How can I get Qt to use the actual proxy I want it to use? 如何让Qt使用我想要它使用的实际代理? All other apps on this machine can use it so why not Qt? 这台机器上的所有其他应用程序都可以使用它,为什么不Qt?

Apparently QML WebView doesn't use Qt proxy settings. 显然QML WebView不使用Qt代理设置。 I don't know if this is the expected behavior or it's just broken. 我不知道这是否是预期的行为,或者它只是被打破了。 Try to set http_proxy environment variable as suggested in this discussion . 尝试按照本讨论中的建议设置http_proxy环境变量。

这段代码需要在第一个Qt对象内部传递,如QMainWindowQWidget ,在构造函数内部是一个不错的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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