I would like to download 1 image and save it. Here is what I have so far:
QCoreApplication a(argc, argv);
QNetworkAccessManager *manager = new QNetworkAccessManager();
QNetworkReply *img = manager->get(QNetworkRequest(QUrl("http://placekitten.com/200/300")));
QImage* img2 = new QImage();
img2->save(img->readAll(), "omg2.png");
But id doesn't work, please help!
EDIT: That also dont work, i get "oops" in console:
QNetworkAccessManager *manager = new QNetworkAccessManager();
//connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
QNetworkReply *img = manager->get(QNetworkRequest(QUrl("http://placekitten.com/200/300")));
QImage* img2开发者_开发问答 = new QImage();
img2->loadFromData(img->readAll());
if(img2->isNull())
std::cout << "oops";
img2->save("omg2.png");
EDIT:
NOw i get strange error with vtable
, after qmake
error still exist:
#include <QtCore/QCoreApplication>
#include <QNetworkAccessManager>
#include <QtNetwork>
#include <QImage>
#include <QPixmap>
#include <iostream>
class omg : public QObject{
Q_OBJECT
public:
void start(){
QNetworkAccessManager *manager = new QNetworkAccessManager();
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
manager->get(QNetworkRequest(QUrl("http://placekitten.com/200/300")));
}
public slots:
void replyFinished(QNetworkReply* reply)
{
//Check for errors first
QImage* img2 = new QImage();
img2->loadFromData(reply->readAll());
if(img2->isNull())
std::cout << "oops";
img2->save("omg2.png");
}
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
omg *o = new omg();
o->start();
return a.exec();
}
You are trying to read data from QNetworkReply even before the Http request is made ,let alone finish.
NetworkAccessManager *manager = new QNetworkAccessManager();
connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
manager->get(QNetworkRequest(QUrl("http://placekitten.com/200/300")));
...
/*Define slot and read data from QNetworkreply*/
void finished(QNetworkReply* reply)
{
//Check for errors first
QImage* img2 = new QImage();
img2->loadFromData(reply->readAll());
if(img2->isNull())
std::cout << "oops";
img2->save("omg2.png");
}
精彩评论