版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/kunyXu/article/details/78864587
QT播放界面设计
今天写了个QT的实时视频播放界面,其实要写一个播放界面非常容易,以下为代码
首先定义一个用于播放的控件:
* PlsyItem.h
#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
- PlsyItem.cpp
#include "PlayItem.h"
PlayItem::PlayItem(QObject *parent) : QObject(parent)
{
}
PlayItem::PlayItem(const QImage &qimg)
{
this->setPixmap(QPixmap::fromImage(qimg));
this->update();
}
PlayItem::~PlayItem()
{
}
void PlayItem::setQImage(const QImage &qimg)
{
this->setPixmap(QPixmap::fromImage(qimg));
this->update();
}
然后定义一个 QGraphicsView,用于显示
* PlayWidget.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
- PlayWidget.cpp
#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();
}
然后将PlayWidget类作为主窗口或者某个窗体的控件即可,当PlayWidget收到QImage,便可以显示出来,而且可以缩放