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
){
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
reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
// Or the target URL if it was a redirect:
reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
// no error received?
if (reply->error() == QNetworkReply::NoError)
{
// read data from QNetworkReply here
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;
}
#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::RedirectionTargetAttribute);
// 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;
}
To copy to clipboard, switch view to plain text mode
.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"
{
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;
int Request;
};
#endif // HTTP_NEW_H
#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
To copy to clipboard, switch view to plain text mode
main code:
#include "http_new.h"
#include <QtGui/QApplication>
int main(int argc, char *argv[])
{
http_new w;
w.showMaximized();
return a.exec();
}
#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();
}
To copy to clipboard, switch view to plain text mode
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.
Bookmarks