天天看点

基于Qt和VS2017的高仿Win7图片浏览器制作

         这几天一直在忙着做一个图片浏览器,花费了很多的时间去调试,最终实现结果和win7自带的图片浏览器基本功能差不多,主要工作在QLabel与图片的大小变化还有翻转的时候大小变化的问题,问题很多,但是都解决了,先看一个作品吧。

基于Qt和VS2017的高仿Win7图片浏览器制作
基于Qt和VS2017的高仿Win7图片浏览器制作
基于Qt和VS2017的高仿Win7图片浏览器制作
基于Qt和VS2017的高仿Win7图片浏览器制作
基于Qt和VS2017的高仿Win7图片浏览器制作

        主要思路就是添加action,给action注册事件,然后在那些前一页,后一页,顺时针翻转。逆时针翻转等的函数中写算法,控制图片动作。

1.接下来是主要的几个函数,先是open:

void PictureBrowsing::on_actionOpen_O_triggered() {
	int g, k;
	filename = QFileDialog::getOpenFileName(this, tr("Select image:"),".", tr("Images (*.png *.bmp *.jpg *.gif)"));
	if (filename.isEmpty())
		return;

	if (!image.load(filename)) {
		QMessageBox::information(this, tr("Error"), tr("Open file error"));
		return;	
	}
	getImInfoList();
	
	imageShow();
	setWindowTitle(QFileInfo(filename).fileName()+tr("-imageViewer"));

}
           

2.其中的imageShow()函数(注意,后面也会用到这个自定义的函数)

void PictureBrowsing::imageShow()
{

	QPixmap pixmap = QPixmap::fromImage(image);
	pixmap = pixmap.scaled(ui.centralWidget->width() - 50, ui.centralWidget->height() - 50, Qt::KeepAspectRatio);
	pix = pixmap;//全局pix
	QSize imagesize = pixmap.size();
	ui.imageLabel->resize(imagesize);//重新调整label大小以适应图片大小
	ui.imageLabel->setPixmap(pixmap);
}
           

3.其中的getImInfoList()函数(此函数主要是将一个文件夹中的所有图片格式的路径指针存储到一个数组imgInfoList中,以便在按下前一页,或者后一页时,取出数组中指针对应的图片)

void PictureBrowsing::getImInfoList() {
	imgInfoList.clear();
	path = QFileInfo(filename).absolutePath();
	QDir dir = QFileInfo(filename).absoluteDir();
	QFileInfoList infoList = dir.entryInfoList(QDir::Files);

	QFileInfo info;
	for (int i = 0; i < infoList.count(); i++) {
		info = infoList.at(i);
		QString suffix = info.suffix();

		if (suffix == "jpg" || suffix == "bmp" || suffix == "png") {
			imgInfoList.append(info);
		}
	}
	QFileInfo curImageInfo = QFileInfo(filename);
	for (int j = 0; j < imgInfoList.count(); j++) {
		info = imgInfoList.at(j);
		if (info.fileName() == curImageInfo.fileName()) {
			index = j;
		}
	}
}
           

4.“上一张”按钮

void PictureBrowsing::on_actionPrevious_P_triggered()
{
	
	index = index - 1;
	int count = imgInfoList.count();
	if (index == -1)
		index = count - 1;
	filename.clear();
	filename.append(path);
	filename += "/";
	filename += imgInfoList.at(index).fileName();

	//QImage image;
	if (!image.load(filename)) {
		QMessageBox::information(this,tr("Error"),tr("Open file error"));
		return;
	}
	imageShow();
	setWindowTitle(QFileInfo(filename).fileName() + tr("-imageViewer"));
}
           

5.“下一张”按钮

void PictureBrowsing::on_actionNext_N_triggered()
{
	index = index + 1;
	int count = imgInfoList.count();
	if (index == count) {
		index = 0;
	}
	filename.clear();
	filename.append(path);
	filename += "/";
	
	filename += imgInfoList.at(index).fileName();

	if (!image.load(filename)) {
		QMessageBox::information(this,tr("Error"),tr("Open file Error"));
		return;
	}
	imageShow();
	setWindowTitle(QFileInfo(filename).fileName() + tr("-imageViewer"));

}
           

6.顺时针旋转

void PictureBrowsing::on_actionClockWise_C_triggered()
{
	
	QMatrix matrix;
	QPixmap pixmap;


	imageAngle += 1;
	imageAngle = imageAngle % 4;
	matrix.rotate(imageAngle*90);

	image.load(filename);

	image = image.transformed(matrix);
	imageShow();


}
           

7.逆时针旋转

void PictureBrowsing::on_actionAntiClockWise_A_triggered()
{
	//QImage imgRotate;
	QMatrix matrix;
	QPixmap pixmap;
	//QImage image;

	imageAngle += 3;
	imageAngle = imageAngle % 4;
	matrix.rotate(imageAngle * 90);

	image.load(filename);
	//imgRotate = image.transformed(matrix);
	image = image.transformed(matrix);
	imageShow();


}
           

8.放大

void PictureBrowsing::on_actionZooming_Out_O_triggered()
{
	
	QMatrix matrix;
	QPixmap pixmap;
	image.load(filename);
	matrix.rotate(imageAngle * 90);
	image = image.transformed(matrix);


	pixmap = QPixmap::fromImage(image);
	pixmap = pixmap.scaled(pix.width()*1.2, pix.height()*1.2, Qt::KeepAspectRatio);
	pix = pix.scaled(pix.width()*1.2, pix.height()*1.2, Qt::KeepAspectRatio);
	QSize imagesize = pixmap.size();
	ui.imageLabel->setPixmap(pixmap);

}
           

9.缩小

void PictureBrowsing::on_actionZooming_in_I_triggered()
{
	
	 QMatrix matrix;
	 QPixmap pixmap;
	 image.load(filename);
	 matrix.rotate(imageAngle * 90);
	 image = image.transformed(matrix);
	 pixmap = QPixmap::fromImage(image);
	 pixmap = pixmap.scaled(pix.width()*0.8, pix.height()*0.8, Qt::KeepAspectRatio);
	 pix = pix.scaled(pix.width()*0.8, pix.height()*0.8, Qt::KeepAspectRatio);
	QSize imagesize = pixmap.size();
	ui.imageLabel->setPixmap(pixmap);
}
           

好了,鉴于时间原因,我把源代码都放上去了,如果有需要全部代码的,可以到这里下载,注意,我是用Qt+VS,用Qt Creator也可以参考,

                                                 谢谢大家!我的表演结束!

继续阅读