控件
pro文件
修改
QT += core gui multimediawidgets multimedia
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
dialog.cpp
HEADERS += \
dialog.h
FORMS += \
dialog.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
头文件
#ifndef DIALOG_H
#define DIALOG_H
#pragma execution_character_set("utf-8")
#include <QDialog>
#include <QMediaPlayer>
#include<QvideoWidget>
#include<QGraphicsvideoItem>
#include<QFileDialog>
#include<QGraphicsScene>
#include<QMessageBox>
#include<QMediaPlaylist>
QT_BEGIN_NAMESPACE
namespace Ui { class Dialog; }
QT_END_NAMESPACE
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = nullptr);
~Dialog();
private:
Ui::Dialog *ui;
QMediaPlayer *player;//播放对象
QGraphicsVideoItem *videoitem;//播放场景
QString durationtime,psitiontime;//播放时间
private slots:
void onStateChanged(QMediaPlayer::State state);
void onDurationChanged(qint64 duration);
void onPositionChanged(qint64 position);
void on_Open_clicked();
void on_Player_clicked();
void on_Pause_clicked();
void on_stop_clicked();
void on_Sound_clicked();
};
#endif // DIALOG_H
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::Dialog)
{
ui->setupUi(this);
//固定窗口尺寸(又用户设计窗口为准)
setFixedSize(width(),height());
//UI样式设计
//视频播器初始化参数
player=new QMediaPlayer(this);
player->setNotifyInterval(2000);
QGraphicsScene *sence=new QGraphicsScene(this);
ui->g->setScene(sence);
videoitem=new QGraphicsVideoItem;
videoitem->setSize(QSize(ui->g->width(),ui->g->height()));
videoitem->setFlags(QGraphicsItem::ItemIsMovable|
QGraphicsPathItem::ItemIsSelectable|
QGraphicsItem::ItemIsFocusable);
sence->addItem(videoitem);
player->setVideoOutput(videoitem);
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::onStateChanged(QMediaPlayer::State state){}
void Dialog::onDurationChanged(qint64 duration){}
void Dialog::onPositionChanged(qint64 position){}
//打开寻找视频文件
void Dialog::on_Open_clicked()
{
QString strCurrentPath=QDir::homePath();//获取系统当前目录
QString strDlgTitle="";//打开对话框的标题名称
QString strFilter="MP4 Files(*.mp4);;All Files(*.*);";//打开视频文件过滤器
QString strAllFiles=QFileDialog::getOpenFileName(this,strDlgTitle,strCurrentPath,strFilter);
//
//QMessageBox::information(this,"提示",strFilter,QMessageBox::Yes|QMessageBox::No);
if(strAllFiles.isEmpty())
{
QMessageBox::information(this,"提示",strFilter,QMessageBox::Yes|QMessageBox::No);
return;
}
QFileInfo FileInfo(strAllFiles);//获取文件信息
ui->label1->setText(FileInfo.fileName());//修改UI的文件名
player->setMedia(QUrl::fromLocalFile(strAllFiles));
player->play();
}
void Dialog::on_Player_clicked()
{
}
void Dialog::on_Pause_clicked()
{
}
void Dialog::on_stop_clicked()
{
}
void Dialog::on_Sound_clicked()
{
}
main.cpp
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}