QT多线程实现图片以缩略图形式显示

有时候我们需要把打开的图片以缩略图的形式显示出来,下面是一个小例子,可以作为参考

mainwin.h

#ifndef MAINWIN_H

#define MAINWIN_H
 
#include <QWidget>
#include <QTimer>
#include <QImage>
class QPushButton;
class QLabel;
 
class MainWin:public QWidget
{
Q_OBJECT
public:
	MainWin(QWidget*p = NULL, Qt::WindowFlags f = 0);
	~MainWin();
protected slots:
	void openFiles();
	void deleteThread();
	void showError(const QString filename);
	void addThumbnail(QImage);
private:
	void createThumbnail(const QString& filename);
	QWidget* previewwidget;
	int waitseconds;
};
 
#endif

 

thumbnailthread.h

#ifndef TNTHREAD_H

#define TNTHREAD_H
#include <QThread>
#include <QImage>
#include <QString>
 
class ThumbnailThread: public QThread
{
Q_OBJECT
public:
	ThumbnailThread(const QString filename, int n = 1);
	int waitseconds(){return number;};
 
private:
	void run();
	QImage bigpm, smallpm;
	QString pmfilename;
	int number;
 
signals:
	void thumbnailFinished(QImage);
	void thumbnailFailed(const QString filename);
};
#endif

 

mainwin.cpp

#include <QDebug>

#include <QLabel>
#include <QPalette>
#include <QLayout>
#include <QFileDialog>
#include <QPushButton>
#include <QMessageBox>
#include "mainwin.h"
#include "thumbnailthread.h"
 
MainWin::MainWin(QWidget*p, Qt::WindowFlags f)
:QWidget(p,f)
{
	waitseconds = 1;
	QPalette pal = palette();
        pal.setColor(QPalette::Background, Qt::white);
	
	setFixedSize(600,400);
	setWindowTitle("www.cuteqt.com");
	previewwidget = new QWidget(this);	
	previewwidget->setFixedSize(600,350);
	previewwidget->setPalette(pal);
	previewwidget->setAutoFillBackground(true);
 
	QPushButton* btn = new QPushButton("Open pictures..", this);
	btn->setGeometry(450, 360, 130, 35);
	connect(btn, SIGNAL(clicked()), this, SLOT(openFiles()));
 
	QGridLayout* layout = new QGridLayout(previewwidget);
}
 
MainWin::~MainWin()
{
}
 
void MainWin::openFiles()
{
	waitseconds = 1;//reset
 
	QStringList list = QFileDialog::getOpenFileNames(this, "Open one or more pictures", "", "*.jpg *.png *.bmp");
	if( list.count() == 0)
	{
		return;
	}
 
	//open files one by one, and create thread to create thumbnail for each file
	QStringList::Iterator it = list.begin();
	while(it != list.end()) 
	{
     		createThumbnail(*it);
     		++it;
 	}
}
 
void MainWin::createThumbnail(const QString& filename)
{
	qDebug() << filename;
	QThread* thread = new ThumbnailThread(filename, 10 - waitseconds);
	waitseconds ++;
	connect(thread, SIGNAL(thumbnailFinished(QImage)), this, SLOT(addThumbnail(QImage)));
	connect(thread, SIGNAL(thumbnailFailed(const QString)), this, SLOT(showError(const QString)));
	connect(thread, SIGNAL(finished()), this, SLOT(deleteThread()));
	thread->start();
}
 
void MainWin::deleteThread()
{
	QObject* obj = sender();
	ThumbnailThread * th = qobject_cast<ThumbnailThread*>(obj);
	qDebug() << "delete thread..." << th->waitseconds();
	obj->deleteLater();
}
 
void MainWin::addThumbnail(QImage smallpm)
{
	static int i = 0;
	static int j = 0;
 
        qWarning() << "thumbnail create successfully..." << j << i ;
	qWarning() << "Small PM:" << smallpm << smallpm.size();
	QLabel* label = new QLabel;
	label->setPixmap(QPixmap::fromImage(smallpm));
	QGridLayout* gl = qobject_cast<QGridLayout*>(previewwidget->layout());
        gl->addWidget(label, j, i);
	label->show();
	qWarning() << "Label:" <<label << label->isVisible();
	i++;
	if( i > previewwidget->width() / smallpm.width()) 
	{
		i = 0;
		j ++;
	}
}
 
void MainWin::showError(const QString filename)
{
	QMessageBox::information(this, "Error!", filename+" is not a valid picture file.");
}
 

 

thumbnailthread.cpp

#include <QDebug>

#include "thumbnailthread.h"
 
const int TN_WIDTH=160;
const int TN_HEIGHT=120;
 
ThumbnailThread::ThumbnailThread(const QString filename, int n)
{
	pmfilename = filename;
	bigpm = QImage(filename);
	number = n;
}
 
void ThumbnailThread::run()
{
	if( bigpm.isNull())
	{
		emit thumbnailFailed(pmfilename);
	}
	else
	{
		qWarning() << "thread start..." << number;
		sleep(number);
		smallpm = bigpm.scaled(TN_WIDTH, TN_HEIGHT, Qt::KeepAspectRatio);
		emit thumbnailFinished(smallpm);
	}
}

 

main.cpp

#include <QApplication>

#include "mainwin.h"
 
int main(int argc, char* argv[])
{
	QApplication app(argc, argv);
	MainWin mw;
	mw.show();
	return app.exec();
}
 

 

实现结果如下:

 

 

如果需要整个工程可以留言,与大家分享

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值