PDA

View Full Version : [Qt4] How to load Url image into QImage?



Gonzalez
27th March 2006, 17:31
Hi to all ,

I want to load an URL image , for example ,
http://mydomain.com/images/image.jpg , into a QImage.

I try some things , but no one works :



QUrl url("http://mydomain.com/images/image.jpg");
QImage image(url.toLocalFile());


or



QHttp url("http://mydomain.com/images/image.jpg");
QImage image;
image.loadFromData(url.readAll());


How to do this?

jrideout
27th March 2006, 17:37
I'm not too familar with qt's networking classes, but a quick scan of the docs shows that the constructor sets the host, but not the whole url. For that you need to use the get method.

See http://doc.trolltech.com/4.1/qhttp.html#get

Gonzalez
28th March 2006, 10:03
Done !!!.



QUrl url("http://mydomain.com/images/image.jpg");
http = new QHttp(this);
connect(http, SIGNAL(requestFinished(int, bool)),this, SLOT(Finished(int,
bool)));
buffer = new QBuffer(&bytes);
buffer->open(QIODevice::WriteOnly);
http->setHost(url.host());
Request=http->get (url.path(),buffer);

public slots:

void Finished(int requestId, bool error){
if (Request=requestId){
QImage img;
img.loadFromData(bytes);
}

private :

QBuffer *buffer;
QByteArray bytes;
QHttp *http;
int Request;

antweb
11th October 2014, 11:05
Hello,

I made use of your code and I am able to access Url images. The issue that i am facing is that I am not able to access all Url images. It works for some images but not for all?
Do you know what might be the issue?

Like for eg.
I am able to acquire this image : http://www.slowstop.com/images/uploads/YouTube_Logo.jpg
But not this one : http://colorlib.com/dazzling/wp-content/uploads/sites/6/2013/03/image-alignment-150x150.jpg
These are two random pics from google images.

Please help me out!

ChrisW67
11th October 2014, 21:33
QHttp has been deprecated for a few years now. You should rewrite your code using QNetworkRequest and related classes. There is at least one HTTP download example in the docs.

Start with a small, self-contained program that only does this one task. If your code still does not behave then please post it here and explain what the problem is.

antweb
13th October 2014, 06:43
I have a pretty straight forward and simple program. I have made use of QNetworkRequest this time.

.cpp code :


#include "http_new.h"
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QAuthenticator>
#include <QNetworkReply>
#include <QtCore/QCoreApplication>
#include <QDebug>
#include <QImageReader>

QNetworkAccessManager* nam;

http_new::http_new(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);

nam = new QNetworkAccessManager(this);
QObject::connect(nam, SIGNAL(finished(QNetworkReply*)),
this, SLOT(finishedSlot(QNetworkReply*)));

QUrl url("http://www.slowstop.com/images/uploads/YouTube_Logo.jpg");
QNetworkReply* reply = nam->get(QNetworkRequest(url));
}

http_new::~http_new()
{

}

void http_new::finishedSlot(QNetworkReply *reply)
{
// Reading attributes of the reply
// e.g. the HTTP status code
QVariant statusCodeV =
reply->attribute(QNetworkRequest::HttpStatusCodeAttribute );

// Or the target URL if it was a redirect:
QVariant redirectionTargetUrl =
reply->attribute(QNetworkRequest::RedirectionTargetAttrib ute);

// no error received?
if (reply->error() == QNetworkReply::NoError)
{
// read data from QNetworkReply here
QByteArray bytes = reply->readAll(); // bytes
QPixmap pixmap;
pixmap.loadFromData(bytes);
ui.label->setPixmap(pixmap);
}
// Some http error received
else
{
// handle errors here
}

// We receive ownership of the reply object
// and therefore need to handle deletion.
delete reply;
}


.h code :


#ifndef HTTP_NEW_H
#define HTTP_NEW_H

#include <QtGui/QMainWindow>
#include <QImage>
#include <QPixmap>
#include <QPainter>
#include <QColor>
#include <QNetworkAccessManager>
#include <QUrl>
#include <QBuffer>
#include <QHttp>
#include <QNetworkRequest>
#include <QNetworkReply>
#include "ui_http_new.h"

class http_new : public QMainWindow
{
Q_OBJECT

public:
http_new(QWidget *parent = 0, Qt::WFlags flags = 0);
~http_new();

public slots:
void finishedSlot(QNetworkReply* reply);

private:
Ui::http_newClass ui;
QImage * image;
QPixmap screenImage;
QPainter * p;
QBuffer *buffer;
QByteArray bytes;
QHttp *http;
int Request;
};

#endif // HTTP_NEW_H


main code:


#include "http_new.h"
#include <QtGui/QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
http_new w;
w.showMaximized();
return a.exec();
}


I am still facing the same issue as i had written earlier.
The URL i have used right now. I am able to view that image in my Label but that is not happening for every image link.
I really cannot understand why?
The second link that I have provided earlier is a working image link through browser but does not give me any image on my Label.

Please Help.

anda_skoa
13th October 2014, 10:10
Have you checked where the problem is?
Do you get no data or does the pixmap loading fail?

Cheers,
_