———————HTTP获取图片———————
🎄效果演示
🎄HTTP请求图片思维导图
🎄添加组件
🎄添加头文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qnetworkaccessmanager.h"
#include "qnetworkrequest.h"
#include "qnetworkreply.h"
#include "QDebug"
#include "qfile.h"
🎄创建对象
QNetworkAccessManager *test;
QNetworkReply *reply;
QFile * file;
🎄关联信号和槽
//关联可读信号
connect(reply, &QIODevice::readyRead, this, &MainWindow::slotReadyRead);
//关联下载信号
connect(reply, &QNetworkReply::downloadProgress, this, &MainWindow::downRepose);
🎄发起请求
//设置链接
QNetworkRequest request;
request.setUrl(QUrl("http://file.alapi.cn/image/comic/120112-15286032729447.jpg"));
//发起请求
reply = test->get(request);
🎄通过槽处理返回的数据
void MainWindow::slotReadyRead()
{
file->write( reply->readAll() );
}
🎄下载完成关闭文件&&显示图片
void MainWindow::downRepose( qint64 bytesReceived, qint64 bytesTotal )
{
qDebug() <<"资源总大小" << bytesTotal << "当前大小" << bytesReceived ;
if( bytesTotal == bytesReceived )
{
file -> close();
QPixmap pic("rlxy.jpg");
pic = pic.scaled( ui->label->size() );
ui->label->setPixmap(pic );
}
}
———————代码获取查看下方———————