qt - 如何通过http下载和保存图像? [英] qt - how to download and save image via http?

查看:571
本文介绍了qt - 如何通过http下载和保存图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在控制台应用程序中下载并保存一些图像与Qt。这里是我到目前为止,(所有的代码都是编译,但运行后,似乎不输入 replyFinished() function ...)

  void Test :: start()
{
std :: cout< start1;
QNetworkAccessManager * manager = new QNetworkAccessManager();
connect(manager,SIGNAL(finished(QNetworkReply *)),this,SLOT(replyFinished(QNetworkReply *)));
manager-> get(QNetworkRequest(QUrl(http://www.exylum.mydevil.net/firefox.jpg)));
}

void Test :: replyFinished(QNetworkReply * reply)
{
std :: cout< st;
QImage * img2 = new QImage();
img2-> loadFromData(reply-> readAll());

if(img2-> isNull())
std :: cout<< oops;

if(img2-> save(omg2.jpg,JPG))
std :: cout< saved;
else
std :: cout<< 别...;
}


解决方案

使用QNetworkAccessManager

头文件

  #ifndef QDOWNLOADER_H 
#define QDOWNLOADER_H

#include< QObject>
#include< QNetworkAccessManager>
#include< QNetworkRequest>
#include< QNetworkReply>
#include< QFile>
#include< QStringList>

class QDownloader:public QObject
{
Q_OBJECT
public:
explicit QDownloader(QObject * parent = 0);
virtual〜QDownloader();
void setFile(QString fileURL);

private:
QNetworkAccessManager * manager;
QNetworkReply * reply;
QFile * file;

私有插槽:
void onDownloadProgress(qint64,qint64);
void onFinished(QNetworkReply *);
void onReadyRead()
void onReplyFinished();
};

#endif // QDOWNLOADER_H

源文件

  #includeqdownloader.h

QDownloader :: QDownloader(QObject * parent):
QObject )
{
manager = new QNetworkAccessManager;
}

QDownloader ::〜QDownloader()
{
manager-> deleteLater();
}

void QDownloader :: setFile(QString fileURL)
{
QString filePath = fileURL;
QString saveFilePath;
QStringList filePathList = filePath.split('/');
QString fileName = filePathList.at(filePathList.count() - 1);
saveFilePath = QString(C:/ Images /+ fileName);

QNetworkRequest请求;
request.setUrl(QUrl(fileURL));
reply = manager-> get(request);

file = new QFile;
file-> setFileName(saveFilePath);
file-> open(QIODevice :: WriteOnly);

connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(onDownloadProgress(qint64,qint64)));
connect(manager,SIGNAL(finished(QNetworkReply *)),this,SLOT(onFinished(QNetworkReply *)));
connect(reply,SIGNAL(readyRead()),this,SLOT(onReadyRead());
connect(reply,SIGNAL(finished()),this,SLOT(onReplyFinished()));
}

void QDownloader :: onDownloadProgress(qint64 bytesRead,qint64 bytesTotal)
{
qDebug(QString :: number(bytesRead).toLatin1()+ + QString :: number(bytesTotal).toLatin1());
}

void QDownloader :: onFinished(QNetworkReply * reply)
{
switch(reply-> error())
{
case QNetworkReply :: NoError:
{
qDebug(文件已成功下载。
} break;
default:{
qDebug(reply-> errorString()。toLatin1());
};
}

if(file-> isOpen())
{
file-> close();
file-> deleteLater();
}
}

void QDownloader :: onReadyRead()
{
file-> write(reply-> readAll());
}

void QDownloader :: onReplyFinished()
{
if(file-> isOpen())
{
file- > close();
file-> deleteLater();
}
}


I am trying to download and save some images with Qt in a console appplication. Here is what I got so far, (all code is compiling, but after run, it seems to don't enter to replyFinished() function...)

void Test::start()
{
    std::cout << "start1";
    QNetworkAccessManager *manager = new QNetworkAccessManager();
    connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
    manager->get(QNetworkRequest(QUrl("http://www.exylum.mydevil.net/firefox.jpg")));
}

void Test::replyFinished(QNetworkReply* reply)
{
    std::cout << "st";
    QImage* img2 = new QImage();
    img2->loadFromData(reply->readAll());

    if(img2->isNull())
        std::cout << "oops";

    if(img2->save("omg2.jpg", "JPG"))
        std::cout << "saved";
    else
        std::cout << "dont...";
}

解决方案

Download Image using QNetworkAccessManager

header file

#ifndef QDOWNLOADER_H
#define QDOWNLOADER_H

#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QFile>
#include <QStringList>

class QDownloader : public QObject
{
    Q_OBJECT
public:
    explicit QDownloader(QObject *parent = 0);
    virtual ~QDownloader();
    void setFile(QString fileURL);

private:
    QNetworkAccessManager *manager;
    QNetworkReply *reply;
    QFile *file;

private slots:
    void onDownloadProgress(qint64,qint64);
    void onFinished(QNetworkReply*);
    void onReadyRead();
    void onReplyFinished();
};

#endif // QDOWNLOADER_H

source file

#include "qdownloader.h"

QDownloader::QDownloader(QObject *parent) :
    QObject(parent)
{
    manager = new QNetworkAccessManager;
}

QDownloader::~QDownloader()
{
    manager->deleteLater();
}

void QDownloader::setFile(QString fileURL)
{
    QString filePath = fileURL;
    QString saveFilePath;
    QStringList filePathList = filePath.split('/');
    QString fileName = filePathList.at(filePathList.count() - 1);
    saveFilePath = QString("C:/Images/" + fileName );

    QNetworkRequest request;
    request.setUrl(QUrl(fileURL));
    reply = manager->get(request);

    file = new QFile;
    file->setFileName(saveFilePath);
    file->open(QIODevice::WriteOnly);

    connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(onDownloadProgress(qint64,qint64)));
    connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(onFinished(QNetworkReply*)));
    connect(reply,SIGNAL(readyRead()),this,SLOT(onReadyRead()));
    connect(reply,SIGNAL(finished()),this,SLOT(onReplyFinished()));
}

void QDownloader::onDownloadProgress(qint64 bytesRead,qint64 bytesTotal)
{
    qDebug(QString::number(bytesRead).toLatin1() +" - "+ QString::number(bytesTotal).toLatin1());
}

void QDownloader::onFinished(QNetworkReply * reply)
{
    switch(reply->error())
    {
        case QNetworkReply::NoError:
        {
            qDebug("file is downloaded successfully.");
        }break;
        default:{
            qDebug(reply->errorString().toLatin1());
        };
    }

    if(file->isOpen())
    {
        file->close();
        file->deleteLater();
    }
}

void QDownloader::onReadyRead()
{
    file->write(reply->readAll());
}

void QDownloader::onReplyFinished()
{
    if(file->isOpen())
    {
        file->close();
        file->deleteLater();
    }
}

这篇关于qt - 如何通过http下载和保存图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆