[Qt-qml] NetworkAccessManager
Martin Jones
martin.jones at nokia.com
Tue Apr 27 03:19:43 CEST 2010
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/qdeclarativenetworkaccessmanagerfact
> 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
More information about the Qt-qml
mailing list