向QVideoWidget添加按钮 [英] Add button to QVideoWidget

查看:517
本文介绍了向QVideoWidget添加按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家!我尝试为QMediaPlayer元素设置click属性,但是我找不到创建该属性的模式,如果我尝试在Video前面放置一个按钮,即使使用

,该按钮也放置在video后面

button->raise();
videoWidget->lower();

如果我将按钮"全屏显示,屏幕会变成黑色,并且不显示视频

此ID为视频播放器的代码

QMediaPlayer *player = new QMediaPlayer(this);
QVideoWidget *vw = new QVideoWidget(this);

QMediaPlaylist *PlayList = new QMediaPlaylist(this);
PlayList->addMedia(QUrl::fromLocalFile("/home/user/Videos/video.mp4"));
PlayList->setPlaybackMode(QMediaPlaylist::Loop);

QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(vw);

player->setVideoOutput(vw);
player->setPlaylist(PlayList);

vw->setGeometry(0,0,800,480);
vw->show();
player->play();

解决方案

一种可能的解决方案是创建一个小部件,其中QVideoWidget通过布局放置,还添加了按钮,并且我们通过事件.

#include <QApplication>
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QPushButton>
#include <QUrl>
#include <QVBoxLayout>
#include <QVideoWidget>

#include <QDebug>

class VideoWidgetButton: public QWidget{
    QPushButton *btn;
    QVideoWidget *vw;
    QMediaPlayer *player;
public:
    VideoWidgetButton(QWidget *parent=Q_NULLPTR):QWidget(parent){
        setLayout(new QVBoxLayout);
        layout()->setContentsMargins(0, 0, 0, 0);

        vw = new QVideoWidget(this);
        btn = new QPushButton(this);
        btn->setIcon(QIcon(":/icons/tux.jpeg"));
        btn->resize(QSize(128, 128));
        btn->setIconSize(QSize(128, 128));

        connect(btn, &QPushButton::clicked, [](){
            qDebug()<<"clicked";
        });

        layout()->addWidget(vw);

        player = new QMediaPlayer(this);
        player->setVideoOutput(vw);

        QMediaPlaylist *playList = new QMediaPlaylist(this);
        playList->addMedia(QUrl("qrc:/video/SampleVideo_1280x720_1mb.mp4"));
        playList->setPlaybackMode(QMediaPlaylist::Loop);
        player->setPlaylist(playList);
        player->play();

    }
protected:
    void resizeEvent(QResizeEvent *ev){
        btn->move(rect().bottomRight()-btn->rect().bottomRight());
        return QWidget::resizeEvent(ev);
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    VideoWidgetButton w;
    w.resize(640, 480);
    w.show();
    return a.exec();
}

完整的示例可以在下面的链接中找到.. >

everyone! I try to set a click property to a QMediaPlayer Element, but I can not find the mode to make it, and if I try to put a button in front to Video, the button puts behind to video, even with

button->raise();
videoWidget->lower();

And If I put a Button to fullscreen the screen turns in black and don't shows the video

this id the code of the video player

QMediaPlayer *player = new QMediaPlayer(this);
QVideoWidget *vw = new QVideoWidget(this);

QMediaPlaylist *PlayList = new QMediaPlaylist(this);
PlayList->addMedia(QUrl::fromLocalFile("/home/user/Videos/video.mp4"));
PlayList->setPlaybackMode(QMediaPlaylist::Loop);

QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(vw);

player->setVideoOutput(vw);
player->setPlaylist(PlayList);

vw->setGeometry(0,0,800,480);
vw->show();
player->play();

解决方案

One possible solution is to create a widget where the QVideoWidget is placed through a layout, the button is also added and we change the position through the resizeEvent() event.

#include <QApplication>
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QPushButton>
#include <QUrl>
#include <QVBoxLayout>
#include <QVideoWidget>

#include <QDebug>

class VideoWidgetButton: public QWidget{
    QPushButton *btn;
    QVideoWidget *vw;
    QMediaPlayer *player;
public:
    VideoWidgetButton(QWidget *parent=Q_NULLPTR):QWidget(parent){
        setLayout(new QVBoxLayout);
        layout()->setContentsMargins(0, 0, 0, 0);

        vw = new QVideoWidget(this);
        btn = new QPushButton(this);
        btn->setIcon(QIcon(":/icons/tux.jpeg"));
        btn->resize(QSize(128, 128));
        btn->setIconSize(QSize(128, 128));

        connect(btn, &QPushButton::clicked, [](){
            qDebug()<<"clicked";
        });

        layout()->addWidget(vw);

        player = new QMediaPlayer(this);
        player->setVideoOutput(vw);

        QMediaPlaylist *playList = new QMediaPlaylist(this);
        playList->addMedia(QUrl("qrc:/video/SampleVideo_1280x720_1mb.mp4"));
        playList->setPlaybackMode(QMediaPlaylist::Loop);
        player->setPlaylist(playList);
        player->play();

    }
protected:
    void resizeEvent(QResizeEvent *ev){
        btn->move(rect().bottomRight()-btn->rect().bottomRight());
        return QWidget::resizeEvent(ev);
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    VideoWidgetButton w;
    w.resize(640, 480);
    w.show();
    return a.exec();
}

The complete example can be found in the following link.

这篇关于向QVideoWidget添加按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆