[Qt-qml] NetworkAccessManager
Jack Wootton
jackwootton at gmail.com
Tue Apr 27 15:35:47 CEST 2010
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");
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>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/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
>
--
Regards
Jack
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt.nokia.com/pipermail/qt-qml/attachments/20100427/70e89105/attachment.html
More information about the Qt-qml
mailing list