Qt 版本:4.8.4 ,需要network模块
最下面有打包
思路:下载到本地,然后打开。
使用QPixmap保存图片文件, 用QFile保存 图片 你会失望的。
代码片段(4)[全屏查看所有代码]
1. [文件] CImage.cpp ~ 1KB 下载(63)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
#include <QtGui/QtGui>
#include "CImage.h"
CImage::CImage(QWidget *parent):QDialog(parent)
{
ImageLabel=
new
QLabel(tr(
"加载中."
));
QVBoxLayout *vboxLayout =
new
QVBoxLayout;
vboxLayout->addWidget(ImageLabel);
setLayout(vboxLayout);
setWindowTitle(tr(
"加载网络图片测试"
));
resize(300, 200);
currentPicture =
new
QPixmap;
//获取网络图片
QNetworkAccessManager *manager;
manager =
new
QNetworkAccessManager(
this
);
connect(manager, SIGNAL(finished(QNetworkReply*)),
this
, SLOT(replyFinished(QNetworkReply*)));
manager->get(QNetworkRequest(QUrl(
"http://www.oschina.net/img/logo_s2.png"
)));
}
void
CImage::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);
//保存图片
//qDebug()<<"picture saved as "<<filename;
currentFileName = filename;
//可以在onpaint中 用QPixmap 显示currentPicture,则这种方法则不需要保存
//显示图片,
QMovie *move =
new
QMovie(currentFileName);
ImageLabel->setMovie(move);
move->start();
}
}
|
2. [文件] CImage.h ~ 600B 下载(46)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
#ifndef CIMAGE_H
#define CIMAGE_H
#include <QtGui/QDialog>
#include <QLabel>
#include <QMovie>
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QPixmap>
class
QLabel;
class
QLineEdit;
class
QPushButton;
class
CImage:
public
QDialog
{
Q_OBJECT
public
:
CImage(QWidget *parent=0);
virtual
~CImage() {}
private
:
QLabel *ImageLabel;
QPushButton *ImageButton;
QPixmap *currentPicture;
QString currentFileName;
public
slots:
void
replyFinished(QNetworkReply *reply);
};
#endif // CIMAGE_H
|
3. [文件] main.cpp ~ 223B 下载(49)
1
2
3
4
5
6
7
8
9
10
|
#include<QtGui/QtGui>
#include"CImage.h"
int
main(
int
argc,
char
*argv[])
{
QApplication app(argc,argv);
QTextCodec::setCodecForTr(QTextCodec::codecForName(
"UTF-8"
));
CImage dlg;
return
dlg.exec();
}
|
4. [文件] CImage.zip ~ 22KB 下载(243)
from:http://www.oschina.net/code/snippet_583625_19562