#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDir>
#include <QString>
#include <QStringList>
#include <QListWidget>
#include <QVBoxLayout>
#include <QListWidgetItem>
#include <mainwindow.h>
#include "ui_mainwindow.h"
#include <QProcess>
#include <QUrl>
#include<QMessageBox>
#include <mythread.h>
#include <windows.h> // windows
#include <QTimer>
//#include <sys/stat.h> //linux
bool play_state;
//是否重新载入视频
bool if_reload=false;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//ui->label->setStyleSheet("border: 3px groove gray; border-radius: 5px; background-color: rgba(255, 193, 245, 0%); ");
ui->label->setStyleSheet("border: 3px groove gray; border-radius: 5px; background-color: white;");
ui->label_player->setStyleSheet("border: 3px groove gray; border-radius: 5px; background-color: white; ");
ui->pushButton_play_and_pause->setEnabled(false);
m_showWidget.setWindowTitle("Image Show");
m_showWidget.setAlignment(Qt::AlignCenter);
this->IsFinishVideo=false;
qApp->processEvents();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
path = QFileDialog::getOpenFileName(this, tr("选择视频"), ".", tr("Image Files(*.flv *.mp4)"));//得到视频路径
//播放状态,true为播放,false为暂停
QFile file(path);
if(!file.open(QIODevice::ReadOnly))//视频路径打不开
{
//QMessageBox::information(NULL, "Title", "Content", QMessageBox::Ok, QMessageBox::Ok);
return;
}
if(if_reload)
{
//重新加载视频时,重置以下变量
delete layout_video;
delete mediaPlayer;
delete videoWidget;
}
if_reload = true;
mediaPlayer = new QMediaPlayer();
videoWidget = new QVideoWidget();
layout_video = new QVBoxLayout;
//设置视频播放区域与Label的边距
layout_video->setMargin(1);
//根据label_player尺寸设置播放区域
mediaPlayer->setMedia(QUrl::fromLocalFile(path));
//play_state为true表示播放,false表示暂停
play_state = true;
ui->pushButton_play_and_pause->setEnabled(true);
ui->pushButton_play_and_pause->setText("暂停");//启用播放/暂停按钮,并将其文本设置为“暂停”
//播放器开启
videoWidget->resize(ui->label_player->size());
layout_video->addWidget(videoWidget);
ui->label_player->setLayout(layout_video);
mediaPlayer->setVideoOutput(videoWidget);
mediaPlayer->play();
connect(mediaPlayer,SIGNAL(stateChanged(QMediaPlayer::State)),this,SLOT(msg()));
}
void MainWindow::msg()//提示处理完毕信息
{
if(mediaPlayer->state()==QMediaPlayer::StoppedState)
{
QMessageBox::information(NULL, "vedio", "视频处理完毕", QMessageBox::Ok, QMessageBox::Ok);
}
}
void MainWindow::on_pushButton_play_and_pause_clicked()
{
//反转播放状态
if(play_state)
{
mediaPlayer->pause();
ui->pushButton_play_and_pause->setText("播放");
}
else
{
mediaPlayer->play();
ui->pushButton_play_and_pause->setText("暂停");
}
play_state = !play_state;
}
void MainWindow::ShowImage()
{
qDebug()<<"调用"<<endl;
this->setWindowTitle("image");
this->setMinimumSize(800, 600);
this->setMaximumSize(1600,1200);
const QSize IMAGE_SIZE(250, 150);
const QSize ITEM_SIZE(250, 150);
// 判断路径是否存在
//m_strPath = "/home/pi/QT_img"; // linux路径
m_strPath = "E:\\img"; //windows路径
QDir dir(m_strPath);//获取当前目录
if (!dir.exists()) {
QMessageBox::information(NULL, "error", "没有此路径");
return;
}
// 设置过滤器
dir.setFilter(QDir::Files | QDir::NoSymLinks);
QStringList filters;
filters << "*.png" << "*.jpg"<<"*.jpeg";
dir.setNameFilters(filters);
m_imgList = dir.entryList();//目录下所有文件和文件夹的名字,返回一个字符串列表
if (m_imgList.count() <= 0) {
return;
}
m_listWidget = new QListWidget(this);
m_listWidget->setIconSize(IMAGE_SIZE);
m_listWidget->setResizeMode(QListView::Adjust);
m_listWidget->setViewMode(QListView::IconMode);
m_listWidget->setMovement(QListView::Static);
m_listWidget->setSpacing(8);
int nowcount = 0;
dir.mkdir(m_strPath);
dir.setFilter(QDir::Files | QDir::NoSymLinks);
filters << "*.png" << "*.jpg"<<"*.jpeg";
dir.setNameFilters(filters);
m_imgList = dir.entryList();//目录下所有文件和文件夹的名字,返回一个字符串列表
//qDebug()<<"m"
if(m_imgList.count() != oldimgList)//有新加入图片
{
qDebug()<<"新加入图片"<<endl;
flag = 0;
oldimgList = m_imgList.count();
}
for (int i = 0; i<m_imgList.count(); ++i)
{
//QPixmap pixmap(m_strPath + "/" + m_imgList.at(i));//linux
QPixmap pixmap(m_strPath + "\\" + m_imgList.at(i)); // windows
// if(pixmap.isNull())//如果图片不存在 不显示 不插入 但要有一个位置
// {
// qDebug("error");
// //QListWidgetItem *listWidgetItem = new QListWidgetItem(QIcon(pixmap.scaled(IMAGE_SIZE)), m_imgList.at(j));
// //listWidgetItem->setSizeHint(ITEM_SIZE);
// //m_listWidget->insertItem(j, listWidgetItem);
// flag = 1;
// QListWidgetItem *listWidgetItem = new QListWidgetItem(QIcon(pixmap.scaled(IMAGE_SIZE)), m_imgList.at(j));
// listWidgetItem->setSizeHint(ITEM_SIZE);
// m_listWidget->insertItem(j, listWidgetItem);
// continue;
// }
QListWidgetItem *listWidgetItem = new QListWidgetItem(QIcon(pixmap.scaled(IMAGE_SIZE)), m_imgList.at(i));
listWidgetItem->setSizeHint(ITEM_SIZE);
m_listWidget->insertItem(i, listWidgetItem);
}
delete ui->label->layout();
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(m_listWidget);
mainLayout->setMargin(0);
//this->setLayout(mainLayout);
ui->label->setLayout(mainLayout);
// 信号与槽
connect(m_listWidget, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(slot_itemClicked(QListWidgetItem*)));
// if(pixmap.isNull())//如果图片不存在 不显示 不插入 但要有一个位置
// {
// qDebug("error");
// //QListWidgetItem *listWidgetItem = new QListWidgetItem(QIcon(pixmap.scaled(IMAGE_SIZE)), m_imgList.at(j));
// //listWidgetItem->setSizeHint(ITEM_SIZE);
// //m_listWidget->insertItem(j, listWidgetItem);
// flag = 1;
// QListWidgetItem *listWidgetItem = new QListWidgetItem(QIcon(pixmap.scaled(IMAGE_SIZE)), m_imgList.at(j));
// listWidgetItem->setSizeHint(ITEM_SIZE);
// m_listWidget->insertItem(j, listWidgetItem);
// continue;
// }
}
//单击某一图片将其显示出来
void MainWindow::slot_itemClicked(QListWidgetItem * item) {
qDebug()<<"item"<<m_listWidget->row(item);//是按顺序的
m_showWidget.setPixmap(QPixmap(m_strPath + "\\" + m_imgList.at(m_listWidget->row(item))));
m_showWidget.show();
//delete maximage;
}
void MainWindow::onTimeout()
{
Isflag();//判断flag
if(flag == 0)
{
this->ShowImage();//flag 为0刷新 有新的图片过来
flag = 1;
}
// this->ShowImage();
}
//判断图片总数有没有变化 变了 flag = 0 此时让label刷新
void MainWindow::Isflag()
{
m_strPath = "E:\\img"; //windows路径
QDir dir(m_strPath);//获取当前目录
dir.setFilter(QDir::Files | QDir::NoSymLinks);
QStringList filters;
filters << "*.png" << "*.jpg"<<"*.jpeg";
dir.setNameFilters(filters);
m_imgList = dir.entryList();//目录下所有文件和文件夹的名字,返回一个字符串列表
//qDebug()<<"m"
if(m_imgList.count() != oldimgList)//有新加入图片
{
qDebug()<<"新加入图片"<<endl;
flag = 0;
oldimgList = m_imgList.count();
}
}
void MainWindow::on_pushButton_3_clicked()
{
//system("python D://Desktop/1.py");
//path 是视频路径变量
//QProcess::execute("python D://Desktop/1.py");
//this->ShowImage();
QTimer *timer = new QTimer();//新建一个定时器对象
connect(timer, SIGNAL(timeout()), this, SLOT(onTimeout()));
timer->start(1000);
// Mythread *mythread=new Mythread();
//mythread->start();
/*while(1){
if(this->IsFinishVideo){
break;
}
}*/
}
转载于:https://www.cnblogs.com/pjc20/p/7707635.html