Qt 简易图片播放器
生活随笔
收集整理的這篇文章主要介紹了
Qt 简易图片播放器
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、前言
使用 Qt 制作了一個簡單的圖片播放器,點擊 "瀏覽按鈕" 瀏覽圖片所在目錄,目錄中的所有圖片縮小圖標和名稱會顯示在左側(cè)的圖片列表中,點擊列表中的圖片項,可以在右側(cè)區(qū)域的標簽上顯示或播放 png、img、gif 等格式圖片。示意圖如下所示:
二、工程代碼
具體代碼如下:
頭文件 imgPlayer.h
#ifndef IMGPLAYER_H #define IMGPLAYER_H#include <QMainWindow> #include <QFileDialog> #include <QMovie> #include <QDebug>namespace Ui { class ImgPlayer; }class ImgPlayer : public QMainWindow {Q_OBJECTpublic:explicit ImgPlayer(QWidget *parent = nullptr);~ImgPlayer();private slots:void on_openImgButton_clicked(); //瀏覽按鈕-點擊槽函數(shù)void on_imgListWidget_clicked(const QModelIndex &index); //圖片列表-點擊槽函數(shù)private:Ui::ImgPlayer *ui;QString m_imgDirPath; // 圖片所在目錄的具體路徑 };#endif // IMGPLAYER_H源文件 imgPlayer.cpp
#include "imgplayer.h" #include "ui_imgplayer.h"ImgPlayer::ImgPlayer(QWidget *parent) :QMainWindow(parent),ui(new Ui::ImgPlayer) {ui->setupUi(this); }ImgPlayer::~ImgPlayer() {delete ui; }//瀏覽按鈕-點擊槽函數(shù) void ImgPlayer::on_openImgButton_clicked() {m_imgDirPath = QFileDialog::getExistingDirectory(this); // 獲取圖片所在目錄的具體路徑ui->imgComboBox->addItem(m_imgDirPath); // 添加打開目錄的絕對路徑并顯示QDir dir(m_imgDirPath);QFileInfoList fileInfoList = dir.entryInfoList(QDir::Files); // 獲取目錄下的文件QString filePath; // 保存圖片圖片的絕對路徑// 在QListWidget中列出目錄下的文件for(int i=0; i<fileInfoList.count(); i++){filePath.clear(); //清除上一次圖片路徑內(nèi)容filePath.append(m_imgDirPath + "/" + fileInfoList.at(i).fileName()); // 獲得圖片文件的絕對路徑if(fileInfoList.at(i).fileName() == "." || fileInfoList.at(i).fileName() == "..") // 跳過這兩個隱藏目錄{continue;}QListWidgetItem *item = new QListWidgetItem(QIcon(filePath), fileInfoList.at(i).fileName()); // 建立圖片縮小圖標ui->imgListWidget->addItem(item); // 把圖片縮小圖標和名稱顯示在列表窗口中} }//圖片列表-點擊槽函數(shù) void ImgPlayer::on_imgListWidget_clicked(const QModelIndex &index) {Q_UNUSED(index);QString imgPath = (m_imgDirPath + "/" + ui->imgListWidget->currentItem()->text());if(imgPath.endsWith(".gif") || imgPath.endsWith(".Gif")) //判斷是否是gif動圖{QMovie *movie =new QMovie(imgPath);ui->imgLabel->setMovie(movie);movie->start();}else{ui->imgLabel->setPixmap(QPixmap(imgPath)); // 在imgLabel標簽上顯示圖片} }ui文件 imgPlayer.ui
參考:
QT制作一個圖片播放器
轉(zhuǎn)載于:https://www.cnblogs.com/linuxAndMcu/p/11347397.html
總結(jié)
以上是生活随笔為你收集整理的Qt 简易图片播放器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt编写输入法源码V2019,未采用Qt
- 下一篇: 【OA外勤签到】软件免费下载