├── 1.png
├── 2.png
├── ImageViewDemo.pro
├── ImageViewDemo.pro.user
├── README.md
├── image_error_page.png
├── image_home_item_default.png
├── imageview.cpp
├── imageview.h
├── main.cpp
├── mainwindow.cpp
├── mainwindow.h
├── resource.qrc
└── 效果图.png
/1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/llf137224350/ImageView/db4c0800979c9679a01878bed3635f3dce532523/1.png
--------------------------------------------------------------------------------
/2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/llf137224350/ImageView/db4c0800979c9679a01878bed3635f3dce532523/2.png
--------------------------------------------------------------------------------
/ImageViewDemo.pro:
--------------------------------------------------------------------------------
1 | #-------------------------------------------------
2 | #
3 | # Project created by QtCreator 2017-08-14T09:03:06
4 | #
5 | #-------------------------------------------------
6 |
7 | QT += core gui network
8 |
9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
10 |
11 | TARGET = ImageViewDemo
12 | TEMPLATE = app
13 |
14 | # The following define makes your compiler emit warnings if you use
15 | # any feature of Qt which as been marked as deprecated (the exact warnings
16 | # depend on your compiler). Please consult the documentation of the
17 | # deprecated API in order to know how to port your code away from it.
18 | DEFINES += QT_DEPRECATED_WARNINGS
19 |
20 | # You can also make your code fail to compile if you use deprecated APIs.
21 | # In order to do so, uncomment the following line.
22 | # You can also select to disable deprecated APIs only up to a certain version of Qt.
23 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
24 |
25 |
26 | SOURCES += \
27 | main.cpp \
28 | mainwindow.cpp \
29 | imageview.cpp
30 |
31 |
32 | HEADERS += \
33 | mainwindow.h \
34 | imageview.h
35 |
36 | RESOURCES += \
37 | resource.qrc
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/ImageViewDemo.pro.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | EnvironmentId
7 | {22f61b1a-e9ec-47e8-aac3-eefbcf48bec0}
8 |
9 |
10 | ProjectExplorer.Project.ActiveTarget
11 | 0
12 |
13 |
14 | ProjectExplorer.Project.EditorSettings
15 |
16 | true
17 | false
18 | true
19 |
20 | Cpp
21 |
22 | CppGlobal
23 |
24 |
25 |
26 | QmlJS
27 |
28 | QmlJSGlobal
29 |
30 |
31 | 2
32 | UTF-8
33 | false
34 | 4
35 | false
36 | 80
37 | true
38 | true
39 | 1
40 | true
41 | false
42 | 0
43 | true
44 | true
45 | 0
46 | 8
47 | true
48 | 1
49 | true
50 | true
51 | true
52 | false
53 |
54 |
55 |
56 | ProjectExplorer.Project.PluginSettings
57 |
58 |
59 |
60 | ProjectExplorer.Project.Target.0
61 |
62 | Desktop Qt 5.9.1 MinGW 32bit
63 | Desktop Qt 5.9.1 MinGW 32bit
64 | qt.591.win32_mingw53_kit
65 | 0
66 | 0
67 | 0
68 |
69 | D:/code/Qt/build-ImageViewDemo-Desktop_Qt_5_9_1_MinGW_32bit-Debug
70 |
71 |
72 | true
73 | qmake
74 |
75 | QtProjectManager.QMakeBuildStep
76 | true
77 |
78 | false
79 | false
80 | false
81 |
82 |
83 | true
84 | Make
85 |
86 | Qt4ProjectManager.MakeStep
87 |
88 | false
89 |
90 |
91 |
92 | 2
93 | 构建
94 |
95 | ProjectExplorer.BuildSteps.Build
96 |
97 |
98 |
99 | true
100 | Make
101 |
102 | Qt4ProjectManager.MakeStep
103 |
104 | true
105 | clean
106 |
107 |
108 | 1
109 | 清理
110 |
111 | ProjectExplorer.BuildSteps.Clean
112 |
113 | 2
114 | false
115 |
116 | Debug
117 |
118 | Qt4ProjectManager.Qt4BuildConfiguration
119 | 2
120 | true
121 |
122 |
123 | D:/code/Qt/build-ImageViewDemo-Desktop_Qt_5_9_1_MinGW_32bit-Release
124 |
125 |
126 | true
127 | qmake
128 |
129 | QtProjectManager.QMakeBuildStep
130 | false
131 |
132 | false
133 | false
134 | false
135 |
136 |
137 | true
138 | Make
139 |
140 | Qt4ProjectManager.MakeStep
141 |
142 | false
143 |
144 |
145 |
146 | 2
147 | 构建
148 |
149 | ProjectExplorer.BuildSteps.Build
150 |
151 |
152 |
153 | true
154 | Make
155 |
156 | Qt4ProjectManager.MakeStep
157 |
158 | true
159 | clean
160 |
161 |
162 | 1
163 | 清理
164 |
165 | ProjectExplorer.BuildSteps.Clean
166 |
167 | 2
168 | false
169 |
170 | Release
171 |
172 | Qt4ProjectManager.Qt4BuildConfiguration
173 | 0
174 | true
175 |
176 |
177 | D:/code/Qt/build-ImageViewDemo-Desktop_Qt_5_9_1_MinGW_32bit-Profile
178 |
179 |
180 | true
181 | qmake
182 |
183 | QtProjectManager.QMakeBuildStep
184 | true
185 |
186 | false
187 | true
188 | false
189 |
190 |
191 | true
192 | Make
193 |
194 | Qt4ProjectManager.MakeStep
195 |
196 | false
197 |
198 |
199 |
200 | 2
201 | 构建
202 |
203 | ProjectExplorer.BuildSteps.Build
204 |
205 |
206 |
207 | true
208 | Make
209 |
210 | Qt4ProjectManager.MakeStep
211 |
212 | true
213 | clean
214 |
215 |
216 | 1
217 | 清理
218 |
219 | ProjectExplorer.BuildSteps.Clean
220 |
221 | 2
222 | false
223 |
224 | Profile
225 |
226 | Qt4ProjectManager.Qt4BuildConfiguration
227 | 0
228 | true
229 |
230 | 3
231 |
232 |
233 | 0
234 | 部署
235 |
236 | ProjectExplorer.BuildSteps.Deploy
237 |
238 | 1
239 | 在本地部署
240 |
241 | ProjectExplorer.DefaultDeployConfiguration
242 |
243 | 1
244 |
245 |
246 | false
247 | false
248 | 1000
249 |
250 | true
251 |
252 | false
253 | false
254 | false
255 | false
256 | true
257 | 0.01
258 | 10
259 | true
260 | 1
261 | 25
262 |
263 | 1
264 | true
265 | false
266 | true
267 | valgrind
268 |
269 | 0
270 | 1
271 | 2
272 | 3
273 | 4
274 | 5
275 | 6
276 | 7
277 | 8
278 | 9
279 | 10
280 | 11
281 | 12
282 | 13
283 | 14
284 |
285 | -1
286 |
287 |
288 |
289 | %{buildDir}
290 | Custom Executable
291 |
292 | ProjectExplorer.CustomExecutableRunConfiguration
293 | 3768
294 | false
295 | true
296 | false
297 | false
298 | true
299 |
300 | 1
301 |
302 |
303 |
304 | ProjectExplorer.Project.TargetCount
305 | 1
306 |
307 |
308 | ProjectExplorer.Project.Updater.FileVersion
309 | 18
310 |
311 |
312 | Version
313 | 18
314 |
315 |
316 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ImageView
2 | # Qt自定义图片显示控件
3 | >* 支持加载显示网络图片(可指定加载中显示的图片,加载失败显示的图片)
4 | >* 支持加载本地图片
5 | >* 加载网络图片时支持缓存到本地
6 | >* 增加图片展示样式 默认、cover、contain具体用法参照mainwindow.cpp或效果图
7 | >* 使用是需要在项目xxx.pro文件引入network
8 |
9 | # 仅用于学习交流,暂时未用于实际项目,再加上本身技术不到位,可能存在着部分问题,可根据需要自行修改。
10 |
--------------------------------------------------------------------------------
/image_error_page.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/llf137224350/ImageView/db4c0800979c9679a01878bed3635f3dce532523/image_error_page.png
--------------------------------------------------------------------------------
/image_home_item_default.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/llf137224350/ImageView/db4c0800979c9679a01878bed3635f3dce532523/image_home_item_default.png
--------------------------------------------------------------------------------
/imageview.cpp:
--------------------------------------------------------------------------------
1 | #include "imageview.h"
2 |
3 | ImageView::ImageView(QWidget *parent)
4 | :QWidget(parent)
5 | {
6 |
7 | this->mState = STATE::loading;
8 | this->mStyle = STYLE::percentage;
9 | //默认当前路径下的cache目录
10 | this->cacheDir = QDir::currentPath()+QDir::separator()+"cache";
11 | QDir dir(this->cacheDir);//判断缓存路径是否存在
12 | if(!dir.exists()){
13 | dir.mkdir(this->cacheDir);
14 | }
15 | this->mManager = new QNetworkAccessManager(this);
16 | connect(this->mManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(slotReplyPixmapLoad(QNetworkReply*)));
17 |
18 | }
19 | void ImageView::slotReplyPixmapLoad(QNetworkReply* reply)
20 | {
21 | if(reply->error() == QNetworkReply::NoError)
22 | {
23 | //获取图片数据
24 | QByteArray bytes = reply->readAll();
25 | QPixmap pixmap;
26 | pixmap.loadFromData(bytes);
27 | this->setSucceedPixmap(pixmap);
28 | //使用路径生成md5
29 | QString md5;
30 | QByteArray bb = QCryptographicHash::hash ( this->mUrl.toUtf8(), QCryptographicHash::Md5 );
31 | md5.append(bb.toHex());
32 | //写入缓存
33 | QFile file(this->cacheDir+QDir::separator() + md5);
34 | if(!file.exists()){
35 | file.open(QIODevice::WriteOnly);
36 | file.close();
37 | }
38 | if (file.open(QIODevice::Append)){
39 | file.write(bytes);
40 | }
41 | file.close();
42 | }else{
43 | this->mState = STATE::failed;
44 | repaint();
45 | }
46 | this->mManager->deleteLater();
47 |
48 | }
49 | void ImageView::paintEvent(QPaintEvent *event)
50 | {
51 | Q_UNUSED(event);
52 | QPainter p(this);
53 | p.setRenderHint(QPainter::Antialiasing, true);
54 |
55 | if(this->mState == STATE::succeed){//加载成功
56 | if(mStyle == STYLE::cover){
57 | //动态计算长的一边 以短的为基准
58 | if( this->succeedPixmap.width() > this->succeedPixmap.height() ){// w / h =mw / h
59 | float w = this->width() * this->succeedPixmap.height() / this->height();
60 | //得道宽度
61 | p.drawPixmap(QRect(0,0,this->width(),this->height()),this->succeedPixmap,QRect((this->succeedPixmap.width() - w ) / 2,0,w,this->succeedPixmap.height()));
62 | }else{
63 | float h = this->height() * this->succeedPixmap.width() / this->width();
64 | //得道宽度
65 | p.drawPixmap(QRect(0,0,this->width(),this->height()),this->succeedPixmap,QRect(0,(this->succeedPixmap.height() - h ) / 2,this->succeedPixmap.width(),h));
66 | }
67 |
68 | }else if(mStyle == STYLE::percentage){//已控件自身为基准
69 | p.drawPixmap(QRect(0,0,this->width(),this->height()),this->succeedPixmap);
70 | }else if(mStyle == STYLE::contain){//以长的为基准
71 | //计算宽高比
72 | if(this->succeedPixmap.width() > this->succeedPixmap.height()){
73 | //计算对应的高度
74 | QPixmap pixmap(this->succeedPixmap.width(), this->height() * this->succeedPixmap.width() / this->width());
75 | pixmap.fill(Qt::white);//用透明色填充
76 | QPainter paint(&pixmap);
77 | paint.drawPixmap(0,(pixmap.height() - this->succeedPixmap.height()) / 2,this->succeedPixmap.width(),this->succeedPixmap.height(),this->succeedPixmap);
78 | p.drawPixmap(QRect(0,0 ,this->width(),this->height()),pixmap);
79 | }else{
80 | QPixmap pixmap(this->width() * this->succeedPixmap.height() / this->height(),this->succeedPixmap.height());
81 | pixmap.fill(Qt::white);//用透明色填充
82 | QPainter paint(&pixmap);
83 | paint.drawPixmap((pixmap.width() - this->succeedPixmap.width()) / 2,0,this->succeedPixmap.width(),this->succeedPixmap.height(),this->succeedPixmap);
84 | p.drawPixmap(QRect(0,0 ,this->width(),this->height()),pixmap);
85 | }
86 | }
87 | }else if(this->mState == STATE::failed){//加载失败
88 | if(mStyle == STYLE::cover){
89 | //动态计算长的一边
90 | if( this->failedPixmap.width() > this->failedPixmap.height() ){// w / h =mw / h
91 | float w = this->width() * this->failedPixmap.height() / this->height();
92 | //得道宽度
93 | p.drawPixmap(QRect(0,0,this->width(),this->height()),this->failedPixmap,QRect((this->failedPixmap.width() - w ) / 2,0,w,this->failedPixmap.height()));
94 | }else{
95 | float h = this->height() * this->failedPixmap.width() / this->width();
96 | //得道宽度
97 | p.drawPixmap(QRect(0,0,this->width(),this->height()),this->failedPixmap,QRect(0,(this->failedPixmap.height() - h ) / 2,this->failedPixmap.width(),h));
98 | }
99 |
100 | }else if( mStyle == STYLE::percentage){
101 | p.drawPixmap(QRect(0,0,this->width(),this->height()),this->failedPixmap);
102 | }else if(mStyle == STYLE::contain){//以长的为基准
103 | //计算宽高比
104 | if(this->failedPixmap.width() > this->failedPixmap.height()){
105 | //计算对应的高度
106 | QPixmap pixmap(this->failedPixmap.width(), this->height() * this->failedPixmap.width() / this->width());
107 | pixmap.fill(Qt::white);//用透明色填充
108 | QPainter paint(&pixmap);
109 | paint.drawPixmap(0,(pixmap.height() - this->failedPixmap.height()) / 2,this->failedPixmap.width(),this->failedPixmap.height(),this->failedPixmap);
110 | p.drawPixmap(QRect(0,0 ,this->width(),this->height()),pixmap);
111 | }else{
112 | QPixmap pixmap(this->width() * this->failedPixmap.height() / this->height(),this->failedPixmap.height());
113 | pixmap.fill(Qt::white);//用透明色填充
114 | QPainter paint(&pixmap);
115 | paint.drawPixmap((pixmap.width() - this->failedPixmap.width()) / 2,0,this->failedPixmap.width(),this->failedPixmap.height(),this->failedPixmap);
116 | p.drawPixmap(QRect(0,0 ,this->width(),this->height()),pixmap);
117 | }
118 | }
119 |
120 | }else {//加载中
121 | if(mStyle == STYLE::cover){
122 | //动态计算长的一边
123 | if( this->loadingPixmap.width() > this->loadingPixmap.height() ){// w / h =mw / h
124 | float w = this->width() * this->loadingPixmap.height() / this->height();
125 | //得道宽度
126 | p.drawPixmap(QRect(0,0,this->width(),this->height()),this->loadingPixmap,QRect((this->loadingPixmap.width() - w ) / 2,0,w,this->loadingPixmap.height()));
127 | }else{
128 | float h = this->height() * this->succeedPixmap.width() / this->width();
129 | //得道宽度
130 | p.drawPixmap(QRect(0,0,this->width(),this->height()),this->loadingPixmap,QRect(0,(this->loadingPixmap.height() - h ) / 2,this->loadingPixmap.width(),h));
131 | }
132 |
133 | }else if(mStyle == STYLE::percentage){//已控件自身为基准
134 | p.drawPixmap(QRect(0,0,this->width(),this->height()),this->loadingPixmap);
135 | }
136 | else if(mStyle == STYLE::contain){//以长的为基准
137 | //计算宽高比
138 | if(this->loadingPixmap.width() > this->loadingPixmap.height()){
139 | //计算对应的高度
140 | QPixmap pixmap(this->loadingPixmap.width(), this->height() * this->loadingPixmap.width() / this->width());
141 | pixmap.fill(Qt::white);//用透明色填充
142 | QPainter paint(&pixmap);
143 | paint.drawPixmap(0,(pixmap.height() - this->loadingPixmap.height()) / 2,this->loadingPixmap.width(),this->loadingPixmap.height(),this->loadingPixmap);
144 | p.drawPixmap(QRect(0,0 ,this->width(),this->height()),pixmap);
145 | }else{
146 | QPixmap pixmap(this->width() * this->loadingPixmap.height() / this->height(),this->loadingPixmap.height());
147 | pixmap.fill(Qt::white);//用透明色填充
148 | QPainter paint(&pixmap);
149 | paint.drawPixmap((pixmap.width() - this->loadingPixmap.width()) / 2,0,this->loadingPixmap.width(),this->loadingPixmap.height(),this->loadingPixmap);
150 | p.drawPixmap(QRect(0,0 ,this->width(),this->height()),pixmap);
151 | }
152 | }
153 | }
154 |
155 | }
156 | //设置获取成功之后的图片
157 | void ImageView::setSucceedPixmap(QPixmap p)
158 | {
159 | this->succeedPixmap = p;
160 | this->mState = STATE::succeed;
161 | this->repaint();
162 | }
163 | //设置加载中的图片
164 | void ImageView::setLoadingPixmap(QPixmap p)
165 | {
166 | this->loadingPixmap = p;
167 | }
168 | //设置加载失败 显示的图片
169 | void ImageView::setFailedPixmap(QPixmap p)
170 | {
171 | this->failedPixmap = p;
172 |
173 | }
174 | //设置显示样式
175 | void ImageView::setDisplayStyle(ImageView::STYLE style)
176 | {
177 | this->mStyle = style;
178 | }
179 | //设置路径
180 | void ImageView::setUrl(QString url)
181 | {
182 | this->mUrl = url;
183 | //判断是否为网络路径
184 | if(this->mUrl.contains("http://") || this->mUrl.contains("https://") ){//网络图片
185 | //判断有没有缓存
186 | QString md5;
187 | QByteArray bb = QCryptographicHash::hash ( this->mUrl.toUtf8(), QCryptographicHash::Md5 );
188 | md5.append(bb.toHex());
189 | QFile file(this->cacheDir+QDir::separator() + md5);
190 | if(!file.exists()){//不存在 走网络
191 | qDebug()<<"网络加载";
192 | this->mState = STATE::loading;
193 | this->repaint();
194 | this->mManager->get(QNetworkRequest(url));
195 | }else{//存在 走本地
196 | if( file.open(QIODevice::ReadOnly)){
197 | QByteArray bytes = file.readAll();
198 | QPixmap pixmap;
199 | pixmap.loadFromData(bytes);
200 | this->setSucceedPixmap(pixmap);
201 | qDebug()<<"本地加载";
202 | }else {
203 | this->mState = STATE::failed;
204 | repaint();
205 | }
206 | }
207 | file.close();
208 | }else{//本地图片
209 | this->setSucceedPixmap(QPixmap(url));
210 | }
211 | }
212 |
213 | //获取加载成功后的图片
214 | QPixmap ImageView::getSucceedPixmap()
215 | {
216 | return this->succeedPixmap;
217 | }
218 |
--------------------------------------------------------------------------------
/imageview.h:
--------------------------------------------------------------------------------
1 | #ifndef IMAGEVIEW_H
2 | #define IMAGEVIEW_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 | #include
13 | /**
14 | * 开发者:い 狂奔的蜗牛
15 | * 描述:支持本地图片加载和网络图片加载的ImageView,并支持缓存。
16 | * 由于第一版并未实际用于项目,可能存在部分问题,可自行修改扩展
17 | */
18 | class ImageView : public QWidget
19 | {
20 | Q_OBJECT
21 | public:
22 | ImageView(QWidget *parent = 0);
23 | enum STATE {
24 | loading,//标示加载中
25 | succeed,//标示加载成功
26 | failed//标示加载失败
27 | };
28 | enum STYLE {
29 | cover,//以长度小的一边为基准 显示中间位置
30 | contain,//完整显示图片 两边留空白
31 | percentage//以控件大小为基准,会失真
32 | };
33 | public slots:
34 | void slotReplyPixmapLoad(QNetworkReply *);
35 | protected:
36 | void paintEvent(QPaintEvent *);//重写绘制方法
37 | private:
38 | //属性
39 | QPixmap loadingPixmap;//加载中
40 | QPixmap succeedPixmap;//加载成功
41 | QPixmap failedPixmap;//加载失败
42 | int mState;//标示状态 加载中 加载成功 加载事变
43 | int mStyle;//标示显示样式
44 | QString mUrl;//路径
45 | QString cacheDir;//缓存目录
46 | QNetworkAccessManager *mManager;
47 | void setSucceedPixmap(QPixmap );//设置加载成功的图片
48 | public:
49 | //方法
50 | void setLoadingPixmap(QPixmap );//设置加载中图片
51 | void setFailedPixmap(QPixmap );//设置加载失败图片
52 | void setDisplayStyle(STYLE);//设置图片显示样式
53 | void setUrl(QString);//图片路径
54 | QPixmap getSucceedPixmap();//获取加载成功后的图片
55 | };
56 |
57 | #endif // IMAGEVIEW_H
58 |
--------------------------------------------------------------------------------
/main.cpp:
--------------------------------------------------------------------------------
1 | #include "mainwindow.h"
2 | #include
3 |
4 | int main(int argc, char *argv[])
5 | {
6 | QApplication a(argc, argv);
7 | MainWindow w;
8 | w.show();
9 |
10 | return a.exec();
11 | }
12 |
--------------------------------------------------------------------------------
/mainwindow.cpp:
--------------------------------------------------------------------------------
1 | #include "mainwindow.h"
2 |
3 | MainWindow::MainWindow(QWidget *parent)
4 | : QMainWindow(parent)
5 | {
6 | this->setMinimumHeight(600);
7 | this->setMinimumWidth(800);
8 | mainWidget = new QWidget;
9 |
10 | //初始化ImageView
11 | imageView = new ImageView(mainWidget);
12 | imageView->setGeometry(50,50,200,200);
13 | imageView->setDisplayStyle(imageView->STYLE::contain);//
14 | imageView->setLoadingPixmap(QPixmap(":/images/image_home_item_default.png"));
15 | imageView->setFailedPixmap(QPixmap(":/images/image_error_page.png"));
16 | imageView->setUrl("http://img.bss.csdn.net/201708102057303085.jpg");
17 |
18 | imageView1 = new ImageView(mainWidget);
19 | imageView1->setGeometry(600,50,200,200);
20 | imageView1->setLoadingPixmap(QPixmap(":/images/image_home_item_default.png"));
21 | imageView1->setFailedPixmap(QPixmap(":/images/image_error_page.png"));
22 | imageView1->setUrl(":/images/2.png");
23 |
24 | imageView2 = new ImageView(mainWidget);
25 | imageView2->setGeometry(300,50,200,300);
26 | imageView2->setDisplayStyle(imageView->STYLE::contain);
27 | imageView2->setLoadingPixmap(QPixmap(":/images/image_home_item_default.png"));
28 | imageView2->setFailedPixmap(QPixmap(":/images/image_error_page.png"));
29 | // imageView2->setUrl("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1502693818433&di=a69ff3920cab95d21304839a26c88150&imgtype=0&src=http%3A%2F%2Fimg1.ph.126.net%2FX4ar4bAt08omFakxCycESg%3D%3D%2F1575696919644252610.jpg");
30 | imageView2->setUrl(":/images/1.png");
31 |
32 | imageView3 = new ImageView(mainWidget);
33 | imageView3->setGeometry(50,300,200,200);
34 | imageView3->setDisplayStyle(imageView->STYLE::cover);
35 | imageView3->setLoadingPixmap(QPixmap(":/images/image_home_item_default.png"));
36 | imageView3->setFailedPixmap(QPixmap(":/images/image_error_page.png"));
37 | imageView3->setUrl("http://img.bss.csdn.net/201708102057303085.jpg");
38 | // imageView3->setUrl(":/images/2.png");
39 |
40 | imageView4 = new ImageView(mainWidget);
41 | imageView4->setGeometry(300,400,200,200);
42 | imageView4->setDisplayStyle(imageView->STYLE::cover);
43 | imageView4->setLoadingPixmap(QPixmap(":/images/image_home_item_default.png"));
44 | imageView4->setFailedPixmap(QPixmap(":/images/image_error_page.png"));
45 | imageView4->setUrl("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1502693818433&di=a69ff3920cab95d21304839a26c88150&imgtype=0&src=http%3A%2F%2Fimg1.ph.126.net%2FX4ar4bAt08omFakxCycESg%3D%3D%2F1575696919644252610.jpg");
46 | // imageView4->setUrl(":/images/1.png");
47 |
48 | this->setCentralWidget(mainWidget);
49 |
50 | }
51 |
52 | MainWindow::~MainWindow()
53 | {
54 |
55 | }
56 |
57 |
--------------------------------------------------------------------------------
/mainwindow.h:
--------------------------------------------------------------------------------
1 | #ifndef MAINWINDOW_H
2 | #define MAINWINDOW_H
3 |
4 | #include
5 |
6 |
7 | #include "imageview.h"
8 |
9 | class MainWindow : public QMainWindow
10 | {
11 | Q_OBJECT
12 |
13 | public:
14 | MainWindow(QWidget *parent = 0);
15 | ~MainWindow();
16 |
17 | private :
18 | QWidget *mainWidget;
19 | ImageView *imageView;
20 | ImageView *imageView1;
21 | ImageView *imageView2;
22 | ImageView *imageView3;
23 | ImageView *imageView4;
24 | };
25 |
26 | #endif // MAINWINDOW_H
27 |
--------------------------------------------------------------------------------
/resource.qrc:
--------------------------------------------------------------------------------
1 |
2 |
3 | 1.png
4 | 2.png
5 | image_error_page.png
6 | image_home_item_default.png
7 |
8 |
9 |
--------------------------------------------------------------------------------
/效果图.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/llf137224350/ImageView/db4c0800979c9679a01878bed3635f3dce532523/效果图.png
--------------------------------------------------------------------------------