如何在半透明的QWidget上播放视频



我想使用QVideoWidget或QGraphicsVideoItem在QWidget上播放具有Qt:: frameesswindowhint标志和Qt::WA_TranslucentBackground属性的电影。但是视频是不可见的。我只听到声音。有什么问题吗?

编辑:

#include "videoplayer.h"
#include <QtWidgets/QApplication>
#include "qboxlayout.h"
#include "qvideowidget.h"
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QWidget *w = new QWidget; 
    w->setWindowFlags(Qt :: Window | Qt::FramelessWindowHint );
    w->setAttribute(Qt::WA_TranslucentBackground, true);
    w->setMinimumSize(300,200);
    QVideoWidget *videoWidget = new QVideoWidget;
    QBoxLayout *controlLayout = new QHBoxLayout;
    controlLayout->setMargin(0);
    controlLayout->addWidget(videoWidget);
    w->setLayout(controlLayout);
    QMediaPlayer mediaPlayer;
    mediaPlayer.setVideoOutput(videoWidget);
    mediaPlayer.setMedia(QUrl::fromLocalFile("C:/1.wmv"));
    videoWidget->show();
    mediaPlayer.play();
    w->show();
    return app.exec();
}

我解决了这个问题。当我们设置WA_TranslucentBackground标志和frameesswindowint属性时,QVideoWidget的QPainter将转到QPainter::CompositionMode_DestinationOver模式,它会导致屏幕上没有任何显示或阴影。在这种情况下,我使用自定义视频小部件,并在创建QPainter painter(this)后的paintEvent;添加

painter.setCompositionMode(QPainter::RasterOp_SourceAndNotDestination); or
painter.setCompositionMode(QPainter::RasterOp_SourceAndDestination);

我在一段时间前实现了VideoWidget。你唯一需要改变的是你的视频路径和设置frameesswindowint标志。

最新更新