在QT中调用本地视频主要使用QMediaPlay进行播放,
首先要在设计界面放一个播放框架,进行布局后进行编程
在源文件里添加需要播放的视频
在QT中.pro文件添加
QT += multimedia multimediawidgets
_player = new QMediaPlayer;
_videoSurface = new baseVideoSurface();
_player->setVideoOutput(_videoSurface);
connect(_videoSurface,&baseVideoSurface::signalShowImage,_videoImageLa,&videoWidget::slotSetImage);
_player->setMedia(QUrl::fromLocalFile("C:/Users/123/Desktop/tuLei.mp4"));
connect(_player,&QMediaPlayer::stateChanged,this,&videoPlayerWidget::slotStateChanged);
#ifndef PLAYITEM_H
#define PLAYITEM_H
#include <QObject>
#include <QGraphicsItem>
#include <QImage>
class PlayItem : public QObject, public QGraphicsPixmapItem
{
Q_OBJECT
public:
explicit PlayItem(QObject *parent = nullptr);
PlayItem(const QImage &qimg);
~PlayItem();
void setQImage(const QImage &qimg);
};
#endif // PLAYITEM_H
下面定义显示的界面
#ifndef PLAYWIDGET_H
#define PLAYWIDGET_H
#include <QGraphicsView>
#include <QGraphicsScene>
#include <opencv2/core/core.hpp>
#include "PlayItem.h"
class PlayWidget : public QGraphicsView
{
Q_OBJECT
public:
PlayWidget();
public slots:
void getResult(const QImage &qimg);
private:
// QImage Mat2QImage(const cv::Mat &img);
void updateScene();
// event
void wheelEvent(QWheelEvent *event);
private:
PlayItem* m_play_item_ptr;
QGraphicsScene* m_img_scene_ptr;
cv::Mat m_img;
double m_zoom_factor;
};
#endif // PLAYWIDGET_H
#include "PlayWidget.h"
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <QWheelEvent>
PlayWidget::PlayWidget()
{
m_img_scene_ptr = new QGraphicsScene();
this->setScene(m_img_scene_ptr);
m_play_item_ptr = new PlayItem();
m_img_scene_ptr->addItem(m_play_item_ptr);
double width_scale = double(this->width()) / double(1280);
double height_scale = double(this->height()) / double(720);
m_zoom_factor = width_scale < height_scale ? width_scale : height_scale;
this->updateScene();
}
void PlayWidget::getResult(const QImage &qimg)
{
m_play_item_ptr->setQImage(qimg);
}
void PlayWidget::updateScene()
{
QMatrix matrix;
matrix.scale(m_zoom_factor, m_zoom_factor);
this->setMatrix(matrix);
}
void PlayWidget::wheelEvent(QWheelEvent *event)
{
if(event->delta() > 0)
m_zoom_factor *= 1.15;
else
m_zoom_factor *= 0.9;
this->updateScene();
}