天天看點

QT開發-HTTP圖檔下載下傳器

1.http使用前提

QT       += core gui network    //必須加上network           

2.必須頭檔案

#include <QNetworkAccessManager>       //三個http必須頭檔案
#include <QNetworkReply>
#include <QNetworkRequest>           

3.widget.h檔案

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QPainter>
#include <QPoint>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();

    QPixmap pixmap;
    QPainter *painter;     //繪制顯示圖檔
    
    QNetworkAccessManager *manager;
    QNetworkReply  *reply;

    void paintEvent(QPaintEvent *event);   //繪圖
    void Init();
private slots:
    void doProcessReadyRead();
    void on_pushButton_clicked();
    void doProceesFinished();
private:
    Ui::Widget *ui;
};

#endif // WIDGET_H
           

4.widget.cpp檔案

#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QFile>
#include <QPixmap>
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    Init();

}

Widget::~Widget()
{
    delete ui;
}

void Widget::Init()
{
    manager = new QNetworkAccessManager(this);
}

void Widget::doProcessReadyRead()                 //讀取儲存圖檔
{
    qDebug()<< "123456";
    QByteArray str = reply->readAll();
    qDebug() << str;
    ui->textEdit->append(str);

    qDebug() << pixmap.loadFromData(str);

    pixmap.save(QString("E:/test.jpg"), "jpg");

    QString path = "E:/1.jpg";                                //打開/建立檔案
    QFile file(path);
          if (!file.open(QIODevice::WriteOnly|QIODevice::Append))
          {
              qDebug() <<"打開失敗";
              return;
          }
          else
          {
              file.write(str);
          }
}

void Widget::paintEvent(QPaintEvent *event)              //繪制圖檔
{
    painter = new QPainter(this);
    QRectF target(0,0, this->width(), this->width());
    QRectF source(0,0, this->width(), this->width());
    QPixmap pixmap_background("E:/1.jpg");
    painter->drawPixmap(target, pixmap_background, source);

    update();
}

void Widget::on_pushButton_clicked()
{
    QNetworkRequest requst;
    requst.setRawHeader(QByteArray("User-Agent"), QByteArray("Sun, 06 Nov 1994 08:49:37 GMT"));   //設定頭
    QString url_str = ui->lineEdit->text();
    QUrl url;
    url.setUrl(url_str);
    requst.setUrl(url);
    reply = manager->get(requst);      //發送請求

    connect(reply, &QIODevice::readyRead, this, &Widget::doProcessReadyRead);            //收到可讀信号
    connect(reply, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),    //收到異常信号
          [=](QNetworkReply::NetworkError code)
            {
                switch((int)code)
                {
                    case QNetworkReply::ConnectionRefusedError:
                        qDebug() << "遠端伺服器拒絕連接配接(伺服器不接受請求)";
                        break;
                    case QNetworkReply::HostNotFoundError:
                        qDebug() << "找不到遠端主機名(無效的主機名)";
                        break;
                    case QNetworkReply::TimeoutError:
                        qDebug() << "與遠端伺服器的連接配接逾時";
                        break;
                    default:
                        break;
                }
            });
    connect(reply, &QNetworkReply::finished,this,&Widget::doProceesFinished);

}

void Widget::doProceesFinished()
{
    qDebug() <<"結束";
}
           

5.widget.ui檔案

QT開發-HTTP圖檔下載下傳器

點選領取Qt學習資料+視訊教程~點選下方連結

http://docs.qq.com/doc/DUlVwTW1FZlZuWE9G

繼續閱讀