PDA

View Full Version : Diplay web image



protocole
25th October 2009, 20:10
Hi all,

I want to display a web image into a QLabel.
this is my code :



void Flux::replyFinished(QNetworkReply* r)
{
/* Récuperation d'un XML et construction du QUrl url_pochette */

QUrl *url_pochette = new QUrl;
url_pochette->setScheme("http");
url_pochette->setHost("host.net");
url_pochette->setPath("/pochette/image.jpg");

r->deleteLater();

QObject::connect(http, SIGNAL(finished(QNetworkReply*)), this, SLOT(load_pochette(QNetworkReply*)));
QNetworkRequest request(*url_pochette);
http->get(request);
}

void Flux::load_pochette(QNetworkReply* r)
{
QImage img;
if (img.load(r, "JPEG"))
{
p->image_pochette->hide();
p->image_pochette->setPixmap(QPixmap::fromImage(img));
p->image_pochette->show();
}
else
cout << "Impossible de charger l'image" << endl;
}


But "Impossible de charger l'image" is display.

Sorry for my english, i'm french.

Thanks for reply.

wysota
25th October 2009, 20:25
I would be nice to at least check if the request ended successfully by checking the status code of the reply.

protocole
26th October 2009, 16:39
I add this line :

if (r->error() == QNetworkReply::NoError) cout << "No error" << endl;

and "no error" is display!

wysota
26th October 2009, 17:42
That's not what I meant. Check the status code of the reply. See QNetworkReply::attribute().

protocole
26th October 2009, 19:42
this line : cout << (r->attribute(QNetworkRequest::HttpStatusCodeAttribute )).toInt() << endl;
display 200

wysota
26th October 2009, 19:52
What about content length?

Also verify that:
1. the result is an image
2. you are able to load a JPEG image from disk using the same code.

wysota
26th October 2009, 19:52
What about content length?

Also verify that:
1. the result is an image
2. you are able to load a JPEG image from disk using the same code.

protocole
27th October 2009, 20:19
Yes i can display a jpeg image load from disk using the same code.

wysota
27th October 2009, 21:32
What about the first issue?

protocole
28th October 2009, 17:17
How can i do the first issue?

wysota
29th October 2009, 09:39
Save the data you get from the reply to disk and try to open it using some image viewer.

protocole
29th October 2009, 11:05
When i do this the file is empty.

wysota
29th October 2009, 11:13
In that case no wonder the image can't be created from it :) You have to correct your request so that it starts returning proper data.

protocole
29th October 2009, 13:02
Finally i use QWebView, it's easier, thanks for all.

radory
10th July 2010, 19:42
Hi protocole, I encouter the same issue as you, stole your code and made some modifies, now it works for me....



#include <QApplication>
#include <QLabel>
#include <QtNetwork/QNetworkAccessManager>
#include <QUrl>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>

static QNetworkReply *reply;

class TOOTLabel : public QLabel
{
Q_OBJECT
public:
TOOTLabel(QWidget *parent =0) : QLabel(parent) {}
public slots:
void TOOTReadyRead()
{
QImage img ;
img.load(reply, "JPEG");
setPixmap(QPixmap::fromImage(img));
show();
resize(img.width(),img.height());
}
};


int main(int argc, char **argv)
{
QApplication app(argc, argv);

QNetworkAccessManager manager;
QNetworkRequest request;
request.setUrl(QUrl("http://www.google.com/intl/en_ALL/images/logo2.gif"));

reply = manager.get(request);
TOOTLabel imgLabel;
QObject::connect( reply, SIGNAL(finished()), &imgLabel, SLOT(TOOTReadyRead()));

return app.exec();
}

#include "main.moc"