Qt World Summit: Register Today!

How to : detect network proxies using Qt

  • My goal is to find all the proxy servers available on a network, using Qt. Another option might be to find the system proxy settings using Qt. However this might be more difficult to do in a clean way, I know that firefox just accesses the registry files when it needs them.

    Anyway, here is the code I have so far:
    #include <QtGui/QApplication>
    #include <QtNetwork>

    int main(int argc, char *argv[])
    QNetworkProxyQuery npq(QUrl("http://www.google.com"));
    QList<QNetworkProxy> listOfProxies = QNetworkProxyFactory::systemProxyForQuery(npq);

    foreach (QNetworkProxy p, listOfProxies) {
        qDebug() << "hostname" << p.hostName();

    } @

    Unfortunately, it only shows one proxy with no hostname, which I suspect is a "no proxy". This seems the most likely as p.type() returns 2

  • You can use QNetworkProxyFactory::setUseSystemConfiguration(true) to have the system settings used everywhere.

    Otherwise the code you have posted looks correct

    You haven't said which platform you're using.
    For windows and mac, the system proxy uses the system APIs and should be working well for most setups. (be aware that WPAD, aka "automatically detect proxy" is slow on windows)

    On linux, the http_proxy environment variable should be used.
    More complex setups using PAC files etc are not currently supported.
    (one possibility is to use libproxy)

    Above information is correct for 4.8 and 5.0 Qt versions

  • I'm trying to create a cross-platform solution. Speed does not matter much for me, since this would be executed once on startup and once only.

    I guess I'm going to give this a go when on the job next time. But in the meantime, a follow up comment/question : is this better/worse than accessing the registry on windows? :o

  • It's the same.
    In fact Qt's implementation does access the registry.
    If the registry says to use a PAC file or WPAD, then windows API is used to do that.
    If you have a static configuration, then it is just returned directly.

  • Alright thanks again! =)

Log in to reply