QT制作一个图片播放器
前言:使用qt制作了一个简单的图片播放器,可以播放gif、png等格式图片
先来看看播放器的功能(当然是很简陋的,没有很深入的设计):
1、点击图片列表中图片进行播放。
2、自动播放,播放的图片的间隔时间可以自己设定,时间的单位是秒。
3、自动播放的时候再点击图片列表会停止自动播放,保存当前播放的图片的顺序,再次点击自动播放的时候将从当前开始。
4、自动播放到最后一张图片的时候将会停止自动播放,再次点击自动播放的时候将会从第一张图片开始。
先上图看看具体功能:
说完功能我们聊聊制作思路和使用到的主要代码:
1、打开有图片的文件,并获取文件的路径
Dir = QFileDialog::getExistingDirectory(this);//获取文件所在的具体路径
2、把文件的路径显示在指定的窗口
ui->photoPath->setText(Dir);//显示打开的文件的具体路径
3、列出文件中的图片的路径,建立小图标,并把图片路径保存到容器中,方便自动播放
//列出目录下的文件for(int i=0;i<fileList.count();i++){QFileInfo info = fileList.at(i);fileDir.clear();fileDir.append(Dir + "/"); QString filename = info.fileName();fileDir.append(filename);photoPath.append(filename);// 把图片的路径保存到容器中if(info.fileName() == "." || info.fileName() == "..") //跳过这两个目录 {continue;}QListWidgetItem *item = new QListWidgetItem(QIcon(fileDir),info.fileName());//建立文件缩小图标ui->photoList->addItem(item);//把图片相对路径显示到窗口中 }
4、单击图片列表中的图片进行播放(图片播放的代码)
tempDir.clear();tempDir.append(Dir+"/");QString path = ui->photoList->currentItem()->text();tempDir.append(path); ui->photoShow->setScaledContents(true);//显示图片的全部ui->photoShow->setPixmap(QPixmap(tempDir));//显示图
5、动态图播放
//播放动态图 void MainWindow::showDinamicPhoto(QString path) {QMovie *movie = new QMovie(path); // path图片路径movie->start(); //开始播放动态图ui->photoShow->setMovie(movie); //将图片设置为为动态ui->photoShow->setScaledContents(true); //尽可能完整的播放整张动图 ,此处要设置为true }
6、自动播放,这里的自动播放我使用了定时器实现
else if(checked) //启动定时器 {delayTime = ui->delayEdit->text();mtime->start(delayTime.toInt()*1000);//启动定时器并设置播放时间间隔autoFlag = true;ui->autoPhoto->setCheckState(Qt::Unchecked);}else if(!checked)//停止定时器 {mtime->stop();//停止定时器 delayTime.clear();autoFlag = false;}
7、设置自动播按钮的状态
ui->autoPhoto->setCheckState(Qt::Unchecked); //把按钮重新置于没有被选中的状态
这里切记要这样使用,不要用setCheckable()函数,很容易出错
具体代码如下:
头文件mainwindow.h
1 #ifndef MAINWINDOW_H 2 #define MAINWINDOW_H 3 4 #include <QMainWindow> 5 #include <QFile> 6 #include <QDir> 7 #include <QTimer> 8 #include <QThread> 9 namespace Ui { 10 class MainWindow; 11 } 12 13 class MainWindow : public QMainWindow 14 { 15 Q_OBJECT 16 17 public: 18 explicit MainWindow(QWidget *parent = 0); 19 ~MainWindow(); 20 21 private slots: 22 void on_pathBt_clicked(); //打开目录 23 24 void on_photoList_clicked(const QModelIndex &index);//单击播放图片 25 26 void on_autoPhoto_clicked(bool checked);//自动播放选择 27 void autoPhoto(); //自动播放函数 28 void showDinamicPhoto(QString path);//动态图播放(格式为gif) 29 30 private: 31 Ui::MainWindow *ui; 32 QFile *file; 33 QString Dir;//打开文件的路径 34 QString tempDir; //照片的绝地路径 35 QVector<QString> photoPath;//存放照片相对路径的容器 36 QTimer *mtime; //定时器 37 QString delayTime; //延时间隔 38 bool autoFlag; //判断是否进入的自动播放格式 39 int num; //照片张数 40 }; 41 42 #endif // MAINWINDOW_H
源文件mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QFileDialog> #include <QDebug> #include <QMessageBox> #include <QMovie>MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),autoFlag(false),ui(new Ui::MainWindow) {ui->setupUi(this);num = 0;delayTime.clear();mtime = new QTimer();//连接自动播放槽函数connect(mtime,SIGNAL(timeout()),this,SLOT(autoPhoto())); }MainWindow::~MainWindow() {delete ui; }void MainWindow::on_pathBt_clicked() {Dir = QFileDialog::getExistingDirectory(this);//获取文件所在的具体路径ui->photoPath->setText(Dir);//显示打开的文件的具体路径 QDir dir(Dir);QStringList file;QFileInfoList fileList = dir.entryInfoList(file,QDir::Files); //获取目录下的文件QString fileDir; //保存图片所在的路径//列出目录下的文件for(int i=0;i<fileList.count();i++){QFileInfo info = fileList.at(i);fileDir.clear();fileDir.append(Dir + "/");QString filename = info.fileName();fileDir.append(filename);photoPath.append(filename);// 把图片的路径装到容器中if(info.fileName() == "." || info.fileName() == "..") //跳过这两个目录 {continue;}QListWidgetItem *item = new QListWidgetItem(QIcon(fileDir),info.fileName());//建立文件缩小图标ui->photoList->addItem(item);//把图片相对路径显示到窗口中 }// qDebug()<<ui->photoList->count(); }//单击图片列表中的图片进行播放,如果当前 void MainWindow::on_photoList_clicked(const QModelIndex &index) {//如果选中了自动播放的情况下,点击列表中的内容,则停止自动播放if(autoFlag) //选中自动播放的情况 {mtime->stop();ui->autoPhoto->setCheckState(Qt::Unchecked);autoFlag = false;}num = ui->photoList->row(ui->photoList->currentItem()); //获取当前点击的内容的行号//在没有选中自动播放的情况下,判断当前是否点击了最后一张照片,如果是最后一张照片,在选中自动播放的情况下让它返回到第一张照片if(!autoFlag){num == ui->photoList->count();num = 0;}tempDir.clear();tempDir.append(Dir+"/");QString path = ui->photoList->currentItem()->text();tempDir.append(path);//判断是否是动态图if(tempDir.endsWith(".gif") || tempDir.endsWith(".Gif")){showDinamicPhoto(tempDir);}else{ui->photoShow->setScaledContents(true);//显示图片的全部ui->photoShow->setPixmap(QPixmap(tempDir));//显示图片 }}//自动播放照片 void MainWindow::on_autoPhoto_clicked(bool checked) {if(ui->delayEdit->text().isEmpty()){QMessageBox::warning(this,"提示","请输入需要间隔的播放时间(秒)");ui->autoPhoto->setCheckState(Qt::Unchecked);return;}else if(ui->photoList->count() == 0){QMessageBox::warning(this,"警告","还没有可以播放的图片");ui->autoPhoto->setCheckState(Qt::Unchecked); //把按钮重新置于没有被选中的状态return;}else if(checked) //启动定时器 {delayTime = ui->delayEdit->text();mtime->start(delayTime.toInt()*1000);//启动定时器并设置播放时间间隔autoFlag = true;//ui->autoPhoto->setCheckState(Qt::Unchecked); }else if(!checked)//停止定时器 {mtime->stop();//停止定时器 delayTime.clear();autoFlag = false;}}void MainWindow::autoPhoto() {//int tempCount=0;//tempCount = photoPath.count(); tempDir.clear();tempDir.append(Dir+"/");QString path = photoPath.at(num); //从容器中找到要播放的照片的相对路径tempDir.append(path); //拼接照片的绝对路径if(tempDir.endsWith(".gif") || tempDir.endsWith(".Gif")){showDinamicPhoto(tempDir);num++;}else if(!(tempDir.endsWith(".gif") || tempDir.endsWith(".Gif"))){ui->photoShow->setScaledContents(true);//显示图片的全部ui->photoShow->setPixmap(QPixmap(tempDir));//显示图片//判断自动播放的时候是否播放到了最后一张图片,如果是则停止自动播放if(num == (photoPath.count()-1)){qDebug()<<num;mtime->stop();num = 0;if(autoFlag){autoFlag = false;}qDebug()<<num;ui->autoPhoto->setCheckState(Qt::Unchecked);//把自动播放按钮置于没有选择的状态 }if(autoFlag){num++;}} }//播放动态图 void MainWindow::showDinamicPhoto(QString path) {QMovie *movie = new QMovie(path); // path图片路径movie->start(); //开始播放动态图ui->photoShow->setMovie(movie); //将图片设置为为动态ui->photoShow->setScaledContents(true); //尽可能完整的播放整张动图 ,此处要设置为true }
界面文件mainwindow.ui
1 <?xml version="1.0" encoding="UTF-8"?> 2 <ui version="4.0"> 3 <class>MainWindow</class> 4 <widget class="QMainWindow" name="MainWindow"> 5 <property name="geometry"> 6 <rect> 7 <x>0</x> 8 <y>0</y> 9 <width>702</width> 10 <height>800</height> 11 </rect> 12 </property> 13 <property name="windowTitle"> 14 <string>MainWindow</string> 15 </property> 16 <widget class="QWidget" name="centralWidget"> 17 <widget class="QWidget" name="layoutWidget"> 18 <property name="geometry"> 19 <rect> 20 <x>10</x> 21 <y>10</y> 22 <width>456</width> 23 <height>49</height> 24 </rect> 25 </property> 26 <layout class="QHBoxLayout" name="horizontalLayout_5"> 27 <item> 28 <layout class="QVBoxLayout" name="verticalLayout"> 29 <property name="spacing"> 30 <number>0</number> 31 </property> 32 <property name="bottomMargin"> 33 <number>0</number> 34 </property> 35 <item> 36 <widget class="QTextBrowser" name="photoPath"> 37 <property name="maximumSize"> 38 <size> 39 <width>150</width> 40 <height>20</height> 41 </size> 42 </property> 43 </widget> 44 </item> 45 <item> 46 <layout class="QHBoxLayout" name="horizontalLayout_2"> 47 <item> 48 <widget class="QPushButton" name="pathBt"> 49 <property name="text"> 50 <string>浏览</string> 51 </property> 52 </widget> 53 </item> 54 <item> 55 <spacer name="horizontalSpacer_3"> 56 <property name="orientation"> 57 <enum>Qt::Horizontal</enum> 58 </property> 59 <property name="sizeHint" stdset="0"> 60 <size> 61 <width>40</width> 62 <height>20</height> 63 </size> 64 </property> 65 </spacer> 66 </item> 67 </layout> 68 </item> 69 </layout> 70 </item> 71 <item> 72 <layout class="QHBoxLayout" name="horizontalLayout_3"> 73 <property name="spacing"> 74 <number>0</number> 75 </property> 76 <item> 77 <spacer name="horizontalSpacer"> 78 <property name="orientation"> 79 <enum>Qt::Horizontal</enum> 80 </property> 81 <property name="sizeHint" stdset="0"> 82 <size> 83 <width>40</width> 84 <height>20</height> 85 </size> 86 </property> 87 </spacer> 88 </item> 89 <item> 90 <layout class="QHBoxLayout" name="horizontalLayout"> 91 <item> 92 <widget class="QLineEdit" name="delayEdit"> 93 <property name="maximumSize"> 94 <size> 95 <width>95</width> 96 <height>16777215</height> 97 </size> 98 </property> 99 <property name="placeholderText"> 100 <string>间隔时间(秒)</string> 101 </property> 102 </widget> 103 </item> 104 <item> 105 <widget class="QCheckBox" name="autoPhoto"> 106 <property name="text"> 107 <string>自动播放</string> 108 </property> 109 </widget> 110 </item> 111 </layout> 112 </item> 113 <item> 114 <spacer name="horizontalSpacer_2"> 115 <property name="orientation"> 116 <enum>Qt::Horizontal</enum> 117 </property> 118 <property name="sizeHint" stdset="0"> 119 <size> 120 <width>40</width> 121 <height>20</height> 122 </size> 123 </property> 124 </spacer> 125 </item> 126 <item> 127 <spacer name="horizontalSpacer_4"> 128 <property name="orientation"> 129 <enum>Qt::Horizontal</enum> 130 </property> 131 <property name="sizeHint" stdset="0"> 132 <size> 133 <width>40</width> 134 <height>20</height> 135 </size> 136 </property> 137 </spacer> 138 </item> 139 </layout> 140 </item> 141 </layout> 142 </widget> 143 <widget class="QWidget" name="layoutWidget"> 144 <property name="geometry"> 145 <rect> 146 <x>12</x> 147 <y>62</y> 148 <width>681</width> 149 <height>461</height> 150 </rect> 151 </property> 152 <layout class="QHBoxLayout" name="horizontalLayout_4"> 153 <item> 154 <widget class="QListWidget" name="photoList"> 155 <property name="sizePolicy"> 156 <sizepolicy hsizetype="Expanding" vsizetype="Minimum"> 157 <horstretch>0</horstretch> 158 <verstretch>0</verstretch> 159 </sizepolicy> 160 </property> 161 <property name="maximumSize"> 162 <size> 163 <width>120</width> 164 <height>400</height> 165 </size> 166 </property> 167 <property name="sizeIncrement"> 168 <size> 169 <width>0</width> 170 <height>0</height> 171 </size> 172 </property> 173 <property name="baseSize"> 174 <size> 175 <width>2</width> 176 <height>0</height> 177 </size> 178 </property> 179 </widget> 180 </item> 181 <item> 182 <widget class="QLabel" name="photoShow"> 183 <property name="maximumSize"> 184 <size> 185 <width>16777215</width> 186 <height>800</height> 187 </size> 188 </property> 189 <property name="text"> 190 <string/> 191 </property> 192 </widget> 193 </item> 194 </layout> 195 </widget> 196 </widget> 197 <widget class="QStatusBar" name="statusBar"/> 198 </widget> 199 <layoutdefault spacing="6" margin="11"/> 200 <resources/> 201 <connections/> 202 </ui>
View Code
具体界面如下
转载于:https://www.cnblogs.com/wurenzhong/p/8057445.html
QT制作一个图片播放器相关推荐
- 使用小程序制作一个音乐播放器
此文主要通过小程序制作一个音乐播放器,实现轮播.搜索.播放.快进.暂停.上一曲.下一曲等功能. 一.创建小程序 二.设计页面 三.接口渲染 一.创建小程序 访问微信公众平台,点击账号注册. 选择小程序 ...
- C#制作一个图片查看器,具有滚轮放大缩小,鼠标拖动,图像像素化,显示颜色RGB信息功能
目录 前言 一.界面设计 二.关键技术 1.把图片拖入到窗体并显示 2.实现图像缩放的功能 3.实现图像的移动效果 4.实时显示当前鼠标处的RGB值 5. 右击功能的实现 6.效果展示 总结 前言 使 ...
- FFmpeg —— Win10下使用Qt制作多功能播放器
Win10下使用Qt制作多功能播放器 视频地址 Win10下使用Qt制作多功能播放器 ...int result = avformat_open_input(&avForm ...
- QT APP实战-图片播放器
一.前言 图片播放器每个人都有用到,鉴于此,打算再造轮子,写一个自己的图片播放器,顺便 巩固所学的知识. 二.整体结构规划 把播放器划分为三个界面. 1> 主界面,也就是打开 ...
- QT——制作简易音频播放器
应用前提:只需要从阿里云数据库中读取选定的某一条实验数据的音频(.wav),现在的测试版本只是播放本地音频,所以做的这个音频播放器只有6个功能:播放.暂停.音量条.静音.进度条.显示当前播放进度的时间 ...
- 用Python制作一个相册播放器(附源码)
对于相册播放器,大家应该都不陌生(用于浏览多张图片的一个应用). 当然还有视频.音乐播放器,同样是用来播放多个视频.音乐文件的. 在Win10系统下,用[照片]这个应用打开一张图片,就可以浏览该图片所 ...
- 【C++】QT制作简单音乐播放器
QT音乐播放器Mymusic 整体布局 写ui文件: Headers Sources 整体布局 创建QT项目,使用Qt5.9版本 分为三栏,最上面放标题和图标,中间放播放的列表,最下面放控制按钮. 引 ...
- QT制作全屏播放器以及出现的问题
使用QT做播放器使用widget::winid可以获取播放器组件的windows句柄并以命令行的方式调起另一个程序使用D3D将内存缓冲区的图像打在上面.但是在做全屏是出现了问题,如果让播放器组件自己独 ...
- 基于C#制作一个音乐播放器
此文主要基于C#制作音乐播放器,可实现导入本地歌曲.音乐播放.音量设置.歌词显示等. 实现流程 1.1.创建项目 1.2.准备素材 1.3.功能开发 实现流程 1.1.创建项目 打开Visual St ...
最新文章
- 漫画:什么是快速排序?(完整版)
- 2021年4月16日 阿里供应链Java研发实习面试(二面)
- Delphi中的指针类型
- Apache Flink vs Apache Spark——感觉二者是互相抄袭啊 看谁的好就抄过来 Flink支持在runtime中的有环数据流,这样表示机器学习算法更有效而且更有效率...
- (一)准备阶段 2019年研究生数学建模D题《汽车行驶工况构建》
- html文件girlfriend,index.html
- 单片机学习--3D动画演示单片机工作原理
- useradd、adduser和userdel在使用时的注意事项
- 以对象的形式动态获取宽高
- (Joomla)字符串截取
- 一个符合SEO优化标准的网站应具备哪些特征?
- linux gd结构体,U-Boot中gd的定义和使用
- 人才盘点最佳实践:45页人才盘点的流程与方法,管理梯队模型
- mac可以写linux的进程,macOS系统上读写Linux的ext4分区方法
- 京东程序员回应“被猝死”;淘宝特价版已提交微信小程序;苹果 M1 单核性能勇超 Intel 11 代 i7...
- 转载一篇心灵鸡汤,致在路上奋斗的ACMer
- 算盘在计算机中的应用,中国大学MOOC: 算盘、计算机都是信息处理的工具。
- 2022-2028全球与中国数字化销售室软件市场现状及未来发展趋势
- SAP中输出质检Q状态库存清单处理实例
- BUUCTF 2021-10-4 Pwn