感谢原文链接 https://blog.csdn.net/wzq806341010/article/details/84553742
.h 文件内容
#ifndef RECIPEL_H
#define RECIPEL_H
#include <QDialog>
#include <QString>
#include <QMovie>
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QPixmap>
namespace Ui {
class Recipel;
}
class Recipel : public QDialog
{
Q_OBJECT
public:
explicit Recipel(QWidget *parent = nullptr);
~Recipel();
public:
void tableWidgetSet();//处方表格设置
void getImg(QString imgUrl);//获取网络图片
public slots:
void replyFinished(QNetworkReply *reply);//获取图片完成 槽
private:
Ui::Recipel *ui;
QPixmap *currentPicture;
QString currentFileName;
};
#endif // RECIPEL_H
cpp 文件内容
#include "recipel.h"
#include "ui_recipel.h"
Recipel::Recipel(QWidget *parent) :
QDialog(parent),
ui(new Ui::Recipel)
{
QString imgUrl = "https://img-blog.csdnimg.cn/20181126175528867.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d6cTgwNjM0MTAxMA==,size_16,color_FFFFFF,t_70";
this->getImg(imgUrl);
}
//显示网络图片
void Recipel::getImg(QString imgUrl)
{
currentPicture = new QPixmap;
//获取网络图片
QNetworkAccessManager *manager;
manager = new QNetworkAccessManager(this);
connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyFinished(QNetworkReply*)));
manager->get(QNetworkRequest(QUrl(imgUrl )));
}
void Recipel::replyFinished(QNetworkReply *reply)
{
if (reply->error() == QNetworkReply::NoError)
{
//获取字节流构造 QPixmap 对象
currentPicture->loadFromData(reply->readAll());
QDateTime now;
QString filename = now.currentDateTime().toString("yyMMddhhmmss.jpg");
currentPicture->save(filename);//保存图片
currentFileName = filename;
//可以在onpaint中 用QPixmap 显示currentPicture,则这种方法则不需要保存
QPixmap tianqi_pixmap = currentPicture->scaled(ui->labelImage->width(), ui->labelImage->height(), Qt::KeepAspectRatio);
//Qt::IgnoreAspectRatio, Qt::SmoothTransformation);//设置图片大小和label的长宽一致
ui->labelImage->setPixmap(tianqi_pixmap);
//显示图片,
QMovie *move = new QMovie(currentFileName);
//ui->lableImg->setMovie(move);
move->start();
}
}