Qml Image downloading error
-
Hi everyone,
i use qmlapplicationviewer to view qml files for my application. of qml files, there a large number of Qml Image Elements with remote sources like:
@Image{
source:"http://xxx.xxx.com/home/an_image.jpg"
}@it works fine on N8 with wifi, but i cant see images with 3G.
Errors in the console
@QML Image: Error downloading http://xxx.xxx.com/home/an_image.jpg- server replied: Forbidden@
I tested the link in the web browser, it works fine.
The most wired problem is that , with 3G, i can dowload files using QNetworkAccessManager. The problem seems that i didn't well configure the "QNetworkAccessManager" of qmlapplicationviewer, or there is some wrong for qml network transparency?
Anyone has ideas?
Thanks
-
it seems the same problem...
"another thread":http://developer.qt.nokia.com/forums/viewthread/2672/ -
Hi
There is indeed some user agent filtering issue with operator network. To bypass this issue, you'll have to define a custom network access manager factory (subclass) and set it in the context of your declarative engine ("link":http://doc.qt.nokia.com/4.7-snapshot/qdeclarativeengine.html#setNetworkAccessManagerFactory) :
@
class NetworkAccessManagerFactory : public QDeclarativeNetworkAccessManagerFactory
{
public:
explicit NetworkAccessManagerFactory(QString p_userAgent = "");QNetworkAccessManager* create(QObject* parent) { CustomNetworkAccessManager* manager = new CustomNetworkAccessManager(__userAgent, parent); return manager; }
private:
QString __userAgent;
};
@Then you'll have to subclass QNetworkAccessManager in order to hook the creation of http request :
@
class CustomNetworkAccessManager : public QNetworkAccessManager
{
Q_OBJECT
public:
explicit CustomNetworkAccessManager(QString p_userAgent = "", QObject *parent = 0);protected:
QNetworkReply *createRequest( Operation op, const QNetworkRequest &req, QIODevice * outgoingData=0 )
{QNetworkRequest new_req(req); new_req.setRawHeader("User-Agent", __userAgent.toAscii()); QNetworkReply *reply = QNetworkAccessManager::createRequest( op, new_req, outgoingData ); return reply; }
private:
QString __userAgent;
};
@Then you can define a helper class to retrieve the same user agent as the one used by Qt webkit :
@
class UserAgentProvider : public QWebPage
{
Q_OBJECT
public:
explicit UserAgentProvider(QWidget *parent = 0);QString getUserAgent() { return userAgentForUrl(QUrl("")); }
};
@Finally you set the network access factory class in your QML context (for instance in main.cpp) :
@
// Set custom NetworkAccessManagerFactory object in QDeclarative context
UserAgentProvider p;
QString userAgent = p.getUserAgent();NetworkAccessManagerFactory factory(userAgent);
viewer.engine()->setNetworkAccessManagerFactory(&factory);
@Nicolas