QT實(shí)戰(zhàn)之實(shí)現(xiàn)圖片瀏覽系統(tǒng)
引言
本系統(tǒng)支持,自動(dòng)播放,左右拖動(dòng)切換,點(diǎn)擊列表切換,點(diǎn)擊按鈕切換;是一個(gè)標(biāo)準(zhǔn)的圖像瀏覽軟件。
Windows 圖片瀏覽器,可以查看當(dāng)前文件夾下的圖片,往上翻、往下翻并且自動(dòng)播放;
此系統(tǒng)增加一個(gè)列表;
實(shí)現(xiàn)功能
1.瀏覽電腦里的文件夾,將當(dāng)前文件夾下的圖片列表羅列出來(lái);
2.鼠標(biāo)點(diǎn)擊列表上的某一張圖片,圖片將顯示出來(lái);
3.可以控制瀏覽當(dāng)前圖片的上一張和下一張;
4.實(shí)現(xiàn)鼠標(biāo)拖動(dòng)圖片,左劃,右劃切換圖片;
5.實(shí)現(xiàn)自動(dòng)播放的開(kāi)始和停止控制。
效果

實(shí)現(xiàn)圖片瀏覽所用知識(shí)
包括窗口部件、布局、事件、對(duì)象模型與容器類、圖形視圖、模型/視圖編程以及多線程等。
實(shí)現(xiàn)流程
1.定義一個(gè)圖片類,該類包含圖片的路徑、文件名、文件id以及獲取這些變量的函數(shù)。
2. 主要包含添加圖像以及獲取所有圖像以及新加入圖像的函數(shù)。
3.最后通過(guò)將圖片名字加入到界面左側(cè)QDockWidget部件中的QTreeView中,
4.通過(guò)雙擊可查看完整圖片,以及通過(guò)滾輪和鼠標(biāo)等事件來(lái)對(duì)圖片進(jìn)行一些操作。
實(shí)現(xiàn)環(huán)境和UI設(shè)計(jì)
環(huán)境:VS2017 + Qt5.12.4

具體實(shí)現(xiàn)
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QListWidget>
#include <QMainWindow>
#include <QTimer>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_btnOpen_clicked();
void on_listWidget_itemClicked(QListWidgetItem *item);
void MyFunction();
void on_pushButton_clicked();
void on_btnLast_clicked();
void on_btnNext_clicked();
protected:
bool eventFilter(QObject *watch, QEvent *evn);
QStringList getFileNames(const QString &path);
private:
Ui::MainWindow *ui;
QVector<QString> mListPath;
QTimer mTimer;
int index ;
};
#endif // MAINWINDOW_Hmainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileInfoList>
#include <QString>
#include <QDir>
#include <QMessageBox>
#include <QImage>
#include "qevent.h"
#include <QDebug>
#pragma execution_character_set("utf-8")
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
index =0;
mTimer.setInterval(1000);
connect(&mTimer,SIGNAL(timeout()),this,SLOT(MyFunction()));
on_btnOpen_clicked();
mTimer.start(1000);
ui->btnOpen->setVisible(false);
// ui->pushButton->setVisible(false);
this->installEventFilter(this);
this->setWindowTitle("圖片瀏覽器");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::MyFunction()
{
if (index == 5) {
index = 0;
} else {
index++;
}
if (index == 5) {
index = 0;
}
// this->setStyleSheet(QString("background-image: url(:/%1.png);").arg(index));
QString strIndex = mListPath.at(index);
qDebug()<<"index "<<QString::number(index)<<"strIndex "<<strIndex;
QDir filePath(ui->inputDirPath->text());
QString fullName = filePath.absoluteFilePath(strIndex);
QImage img(fullName);
QImage thumb = img.scaled(ui->label->width(),ui->label->height(),Qt::KeepAspectRatio);
ui->label->setPixmap(QPixmap::fromImage(thumb));
}
void MainWindow::on_btnOpen_clicked()
{
QString filePath = QCoreApplication::applicationDirPath()+"/pic";
ui->inputDirPath->setText(filePath);
// QMessageBox::information(this,"提示!",filePath);
QDir dir(filePath);
// 判斷文件夾是否存在
if(dir.exists()){
ui->listWidget->clear();
QFileInfoList info_list = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
for(int i =0;i<info_list.count();i++){
ui->listWidget->addItem(info_list.at(i).fileName());
mListPath.push_back(info_list.at(i).fileName());
}
}
else{
QMessageBox::information(this,"提示!","文件夾不存在");
}
}
QStringList MainWindow::getFileNames(const QString &path)
{
QDir dir(path);
QStringList nameFilters;
nameFilters << "*.jpg" << "*.png";
QStringList files = dir.entryList(nameFilters, QDir::Files|QDir::Readable, QDir::Name);
return files;
}
void MainWindow::on_listWidget_itemClicked(QListWidgetItem *item)
{
if(mTimer.isActive())
mTimer.stop();
QDir filePath(ui->inputDirPath->text());
QString fullName = filePath.absoluteFilePath(item->text());
QImage img(fullName);
QImage thumb = img.scaled(ui->label->width(),ui->label->height(),Qt::KeepAspectRatio);
ui->label->setPixmap(QPixmap::fromImage(thumb));
}
//點(diǎn)擊事件函數(shù)
bool MainWindow::eventFilter(QObject *watch, QEvent *evn)
{
static int press_x; //點(diǎn)擊鼠標(biāo)時(shí)獲取的橫坐標(biāo)x
static int press_y; //點(diǎn)擊鼠標(biāo)時(shí)獲取的縱坐標(biāo)y
static int relea_x; //松開(kāi)鼠標(biāo)時(shí)獲取的橫坐標(biāo)x
static int relea_y; //松開(kāi)鼠標(biāo)時(shí)獲取的縱坐標(biāo)y
QMouseEvent *event = static_cast<QMouseEvent *>(evn); //將圖片QT QEvent 轉(zhuǎn)換為 QMouseEvent ,QKeyEvent....等子類
//獲取點(diǎn)擊鼠標(biāo)(手指)時(shí)的坐標(biāo)
if (event->type() == QEvent::MouseButtonPress)
{
press_x = event->globalX();
press_y = event->globalY();
}
//獲取松開(kāi)鼠標(biāo)(手指)時(shí)的坐標(biāo)
if(event->type() == QEvent::MouseButtonRelease)
{
relea_x = event->globalX();
relea_y = event->globalY();
}
//對(duì)鼠標(biāo)(手指)滑動(dòng)的方向進(jìn)行判斷(右滑)
if((relea_x - press_x) > 5 && event->type() == QEvent::MouseButtonRelease && qAbs(relea_y - press_y) < 50)
{
on_btnNext_clicked();
}
if( event->type() == QEvent::MouseButtonRelease)
qDebug()<<"releax "<<QString::number(press_x - relea_x)<<"releay "<<QString::number(relea_y - press_y);
//對(duì)鼠標(biāo)(手指)滑動(dòng)的方向進(jìn)行判斷(左滑)
if((press_x - relea_x) > 5 && event->type() == QEvent::MouseButtonRelease && qAbs(relea_y - press_y) < 50)
{
on_btnLast_clicked();
}
return QWidget::eventFilter(watch, evn);
}
void MainWindow::on_pushButton_clicked()
{
if(ui->pushButton->text()=="滑動(dòng)切換")
{
ui->pushButton->setText("自動(dòng)播放");
if(mTimer.isActive())
mTimer.stop();
}
else
{
ui->pushButton->setText("滑動(dòng)切換");
if(!mTimer.isActive())
mTimer.start();
}
}
void MainWindow::on_btnLast_clicked()
{
if(index == -1)
{
index = 4;
}
else
{
index--;
}
if(index == -1)
{
index = 4;
}
// this->setStyleSheet(QString("background-image: url(:/%1.png);").arg(index));
QString strIndex = mListPath.at(index);
qDebug()<<"index 111"<<QString::number(index)<<"strIndex "<<strIndex;
QDir filePath(ui->inputDirPath->text());
QString fullName = filePath.absoluteFilePath(strIndex);
QImage img(fullName);
QImage thumb = img.scaled(ui->label->width(),ui->label->height(),Qt::KeepAspectRatio);
ui->label->setPixmap(QPixmap::fromImage(thumb));
}
void MainWindow::on_btnNext_clicked()
{
if (index == 5) {
index = 0;
} else {
index++;
}
if (index == 5) {
index = 0;
}
// this->setStyleSheet(QString("background-image: url(:/%1.png);").arg(index)); //切換圖片
QString strIndex = mListPath.at(index);
qDebug()<<"index "<<QString::number(index)<<"strIndex "<<strIndex;
QDir filePath(ui->inputDirPath->text());
QString fullName = filePath.absoluteFilePath(strIndex);
QImage img(fullName);
QImage thumb = img.scaled(ui->label->width(),ui->label->height(),Qt::KeepAspectRatio);
ui->label->setPixmap(QPixmap::fromImage(thumb));
}到此這篇關(guān)于QT實(shí)戰(zhàn)之實(shí)現(xiàn)圖片瀏覽系統(tǒng)的文章就介紹到這了,更多相關(guān)QT圖片瀏覽系統(tǒng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Opencv開(kāi)發(fā)實(shí)現(xiàn)拼圖游戲
這篇文章主要為大家詳細(xì)介紹了Opencv開(kāi)發(fā)實(shí)現(xiàn)拼圖游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
C語(yǔ)言超細(xì)致講解循環(huán)語(yǔ)句
我們說(shuō)到當(dāng)滿足特定條件時(shí),就會(huì)執(zhí)行if語(yǔ)句或者switch語(yǔ)句后面的語(yǔ)句,否則不執(zhí)行,但是這只能執(zhí)行一次,在日常生活中,有些事情是需要重復(fù)去做的,C語(yǔ)句就為此引入了循環(huán)語(yǔ)句。所以今天繼續(xù)為大家分享C語(yǔ)言循環(huán)家族2022-05-05
C++生成隨機(jī)浮點(diǎn)數(shù)的示例代碼
在C++11之前,我們通常采用rand函數(shù)來(lái)生成隨機(jī)數(shù),但rand函數(shù)對(duì)一些情況顯得難以處理。本文將介紹如何利用C++生成隨機(jī)浮點(diǎn)數(shù),需要的可以參考一下2022-04-04
通過(guò)GDB學(xué)習(xí)C語(yǔ)言的講解
今天小編就為大家分享一篇關(guān)于通過(guò)GDB學(xué)習(xí)C語(yǔ)言的講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01
C++11中模板隱式實(shí)例化與顯式實(shí)例化的定義詳解分析
實(shí)例化是為在程序中的函數(shù)模板本身并不會(huì)生成函數(shù)定義,它只是一個(gè)用于生成函數(shù)定義的方案。編譯器使用模板為特定類型生成函數(shù)定義時(shí),得到的是模板實(shí)例。這即是函數(shù)模板的實(shí)例化。而函數(shù)模板實(shí)例化又分為兩種類型:隱式實(shí)例化和顯式實(shí)例化2022-04-04
C語(yǔ)言 if else 語(yǔ)句詳細(xì)講解
本文主要介紹C語(yǔ)言中的if else,這里詳細(xì)介紹了if else 語(yǔ)句并提供了簡(jiǎn)單的示例代碼,希望能幫助編程入門的小伙伴學(xué)習(xí)2016-07-07

