#include "widget.h"
#include "ui_widget.h"
#include<QPixmap>
#include<QFileDialog>
#include<QDebug>
#include<QMessageBox>
#include"playimage.h"
#include<QTimer>
#define LOG 1
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowTitle("图片播放器");
//开局设置暂停按钮不可用,需要调用打开文件成功才可以使用
ui->pauseButton->setEnabled(false);
//设置窗口固定
int width = 800;
int height = 500;
this->setFixedSize(width, height);
}
Widget::~Widget()
{
delete ui;
}
void Widget::sleepMsec(int msec)
{
//创建事件循环对象
QEventLoop loop;
//创建定时器对象
QTimer timer;
timer.setSingleShot(true);
//连接计时器的超时信号到事件循环的退出槽函数
connect(&timer,&QTimer::timeout,&loop,&QEventLoop::quit);
//启动计时器
timer.start(msec);
//阻塞
loop.exec();
}
//定义QString转QPixmap函数
QPixmap Widget::stringToPixmap(QString fileName)
{
QPixmap pixmap(fileName);
return pixmap;
}
void Widget::on_chooseButton_clicked()
{
fileNames = QFileDialog::getOpenFileNames(this, "选择照片", "/", "图像文件(*.jpg *.png *.bmp)");
if (fileNames.isEmpty())
{
QMessageBox::StandardButton reply = QMessageBox::question(this, "警告", "请选择图片",
QMessageBox::Retry | QMessageBox::Cancel);
if (reply == QMessageBox::Retry)
{
// 重新选择图片
on_chooseButton_clicked(); // 递归调用自身再次打开文件选择对话框
}
else
{
// 用户选择取消
}
return;
}
// 图片选择成功的处理
if(!fileNames.isEmpty()) ui->pauseButton->setEnabled(true);
// 默认选择第一个图片
imageTotal = fileNames.count();
QPixmap initPixmap = stringToPixmap(fileNames.first());
if (!initPixmap.isNull()) {
// 转换成功
ui->playImage->updatePixmap(initPixmap);
} else {
// 转换失败
qDebug() << "转为QPixmap失败";
}
}
//暂停功能
void Widget::on_pauseButton_clicked()
{
play = false;
ui->pauseButton->setEnabled(false);
}
//播放功能
void Widget::on_startButton_clicked()
{
play = true;
//没有图片 无法播放
if(imageTotal == 0)
{
#if LOG
qDebug() << "开始播放前没有图片";
#endif
QMessageBox::warning(this,"警告","没有图片播放");
}
//有图片播放
//先打开暂停播放的功能
ui->pauseButton->setEnabled(true);
while(play && imageTotal!=0)
{
imageIndex = (imageIndex + 1) % imageTotal;
//隔一会更新图片
sleepMsec(1000);
ui->playImage->updatePixmap(stringToPixmap(fileNames.at(imageIndex)));
}
}
//向前按钮实现
void Widget::on_pre_btn_clicked()
{
//循环播放
imageIndex = (imageIndex - 1 + imageTotal) % imageTotal;
ui->playImage->updatePixmap(stringToPixmap(fileNames.at(imageIndex)));
}
//向后按钮实现
void Widget::on_ne_btn_clicked()
{
imageIndex = (imageIndex + 1) % imageTotal;
ui->playImage->updatePixmap(stringToPixmap(fileNames.at(imageIndex)));
}
没有合适的资源?快使用搜索试试~ 我知道了~
PlayImage-Demo
共15个文件
png:6个
cpp:3个
h:2个
需积分: 0 5 下载量 111 浏览量
2023-12-14
15:03:41
上传
评论
收藏 431KB RAR 举报
温馨提示
Qt和c++实现图片播放器,支持多种格式图片播放显示,采用PlayImage自定义控件
资源推荐
资源详情
资源评论
收起资源包目录
PlayImage_Demo.rar (15个子文件)
playimage.cpp 966B
widget.ui 4KB
widget.cpp 3KB
PlayImage.pro 1KB
res
behind.png 2KB
forward.png 3KB
background.png 409KB
open.png 3KB
start.png 3KB
pause.png 4KB
main.cpp 175B
PlayImage.pro.user 24KB
res.qrc 284B
playimage.h 510B
widget.h 993B
共 15 条
- 1
资源评论
WANGYB03
- 粉丝: 20
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功