[Qt-qml] NetworkAccessManager
Martin Jones
martin.jones at nokia.com
Thu Apr 29 08:01:37 CEST 2010
On Wed, 28 Apr 2010 11:12:11 pm ext Jack Wootton wrote:
> To follow up on this. I have been reading the documentation for
> QNetworkProxyFactory:
> http://doc.qt.nokia.com/4.7-snapshot/qnetworkproxyfactory.html
>
> For the method
>
> QList<QNetworkProxy> QNetworkProxyFactory::systemProxyForQuery ( const
> QNetworkProxyQuery & query = QNetworkProxyQuery() ) [static]
>
> it is documented that: "On Windows, this function will use the WinHTTP DLL
> functions. Despite its name, Microsoft suggests using it for all
> applications that require network connections, not just HTTP. This will
> respect the proxy settings set on the registry with the proxycfg.exe tool.
> If those settings are not found, this function will attempt to obtain
> Internet Explorer's settings and use them."
>
> I'm have set my proxy using proxycfg tool on Windows, however I still get
> no proxy results from QNetworkProxyFactory::systemProxyForQuery
QNetworkProxyFactory::systemProxyForQuery() requires a QNetworkProxyQuery to
be passed to it. This would also require using a QNetworkProxyFactory, rather
than simply setting a proxy for the QNetworkAccessManager. If you want to do
this then you can see the qml runtime source for inspiration -
tools/qml/qmlruntime.cpp (search for SystemProxyFactory).
To begin, I think you should get the code below working, though. I suspect
the reason it does not work is that you have not provided a valid host name:
proxy.setHostName("my.proxy.com<http://my.proxy.com>");
changing it to this should make it work:
proxy.setHostName("my.proxy.com");
assuming my.proxy.com is a valid proxy.
I've added a simple example to examples/declarative/proxyviewer, illustrating
how to use a QDeclarativeNetworkAccessManagerFactory (should appear on
gitorious within 24hrs). The code is essentially the same as you have below,
so with a correct proxy set this should work.
Martin.
> On Tue, Apr 27, 2010 at 2:35 PM, Jack Wootton
> <jackwootton at gmail.com<mailto:jackwootton at gmail.com>> wrote: Hello,
>
> I have modifed the my implementation of
> QDeclarativeNetworkAccessManagerFactory (question follows code) :)
>
> MyNetworkAccessManagerFactory.h
> ===========================
> class MyNetworkAccessManagerFactory : public
> QDeclarativeNetworkAccessManagerFactory {
> public:
> MyNetworkAccessManagerFactory();
> ~MyNetworkAccessManagerFactory();
>
> public:
> QNetworkAccessManager* create ( QObject * parent );
> };
>
>
> MyNetworkAccessManagerFactory.cpp
> =============================
> MyNetworkAccessManagerFactory::MyNetworkAccessManagerFactory()
> {
> }
>
> MyNetworkAccessManagerFactory::~MyNetworkAccessManagerFactory()
>
> {
> }
>
> QNetworkAccessManager* MyNetworkAccessManagerFactory::create ( QObject*
> parent ) {
> QNetworkProxy proxy;
> proxy.setHostName("my.proxy.com<http://my.proxy.com>");
>
> proxy.setPort(3128);
>
> // Is this static method call acceptable for a re-entrant method?
> // QNetworkProxy::setApplicationProxy(proxy);
>
> QNetworkAccessManager* nam = new QNetworkAccessManager(parent);
>
>
> nam->setProxy(proxy);
> return nam;
> }
>
>
>
> I have a question about ensuring the method create(QObject* parent) is
> thread safe / re-entrant: I assumed the method call
> 'QNetworkProxy::setApplicationProxy(proxy);' is not ok since it is static
> and should not be called from a re-entrant function. Is this the case?
> If so, is it a problem that it is not called?
>
> I made the changes, but I still don't see the webpage loaded in the QML
> document, but I'll deal with that after I know my code is correct first :)
>
>
>
> On Tue, Apr 27, 2010 at 2:19 AM, Martin Jones
> <martin.jones at nokia.com<mailto:martin.jones at nokia.com>> wrote:
>
> On Tue, 27 Apr 2010 01:18:56 am ext Jack Wootton wrote:
> > Hi,
> >
> > I'm trying to set some proxy settings for a QML application - it's not
> > working as expected. I'm unsure the API is being used correctly, can
> > someone check my code makes sense? There are 3 files: main.cpp,
> > MyNetworkAccessManagerFactory.h and MyNetworkAccessManagerFactory.cpp
> >
> > I'm using qt 4.7 and followed the documentation
> > (http://doc.qt.nokia.com/4.7-snapshot/qdeclarativenetworkaccessmanagerfac
> > t ory.html) when writing the code. The method
> > MyNetworkAccessManagerFactory::create(QObject* parent) does get called
> > when the QML document is loaded using the QDeclarativeView, so the
> > framework is definitely picking up the new proxy settings.
> >
> > The questions I have are:
> >
> > 1. Is the implementation of MyNetworkAccessManagerFactory::create
> > correct? I assumed because I was subclassing from a class with 'factory'
> > in its name, that it was ok to return the same pointer to
> > NetworkAccessManager.
> >
> > 2. How do I know if the method that calls "create" takes ownership of the
> > returned pointer or not? I assumed it didn't, hence cleaning up in
> > ~MyNetworkAccessManagerFactory();
> >
> > 3. Is there anything obviously wrong with the way I'm using the
> > NetworkAccessManager?
>
> The factory must create a new NAM each time it is called. This is because
> QML creates multiple threads and each must have its own NAM. It is
> important that you set the parent of the NAM created to the parent passed
> in the create() method (which you already do). The NAM will be destroyed
> when its parent is destroyed. I will update the documentation to make
> this clear.
>
> Also take special note of this line from the documentation:
>
> "Note: the create() method may be called by multiple threads, so ensure the
> implementation of this method is reentrant."
>
> --
> Martin
>
>
>
> --
> Regards
> Jack
>
>
>
> --
> Regards
> Jack
--
Martin
More information about the Qt-qml
mailing list