gpt4 book ai didi

c++ - 添加按钮到 QVideoWidget

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:17:57 26 4
gpt4 key购买 nike

example image

各位!我尝试将 click 属性设置为 QMediaPlayer 元素,但我找不到制作它的模式,如果我尝试将按钮放在视频前面,按钮会放在视频后面,即使有

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 通过布局放置,还添加了按钮,我们通过 resizeEvent() 事件更改位置.

#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();
}

enter image description here

完整的例子可以在下面的link中找到.

关于c++ - 添加按钮到 QVideoWidget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48081448/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com