Add Http Proxy support to existing application using QNetworkProxy
Article Metadata
This article shows how to add proxy support at an application level using QNetworkProxy.
Introduction
Suppose you have an existing Qt application that makes use of QtNetwork classes. Qt let you specify a proxy for every connection you create, but there is simple way to set global proxy settings so the whole application will use those settings for any connection it will use.
Idea and implementation
You just need to insert this sample code in your main.cpp (please insert it right before return a.exec();) in the main(...) method. Of course in this way the proxy settings will be hardcoded, but you can always read/save them from a configuration file, using the QSettings class.
QNetworkProxy proxy;
proxy.setType(QNetworkProxy::HttpProxy);
proxy.setHostName("proxy.example.com");
proxy.setPort(1080);
proxy.setUser("username");
proxy.setPassword("password");
QNetworkProxy::setApplicationProxy(proxy);


Hamishwillee - Works for QML apps?
Hi Andrea
Thanks for this article - clear and succinct. I've added in links to the reference documentation, as this makes it easy for people to get even more detail. I will shortly also add ArticleMetaData - this helps people work out how relevant an article is over time.
One question, will this proxy still be used by network connections set up in QML? As a rule Qt apps are now developed using QML for the UI where possible, so while this is useful, making it clear how you integrate your app with QML can further improve it.
Regards
Hamishhamishwillee 03:17, 2 July 2012 (EEST)