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

Qt 如何在半透明QWidget上播放视频?,qt,qwidget,translucency,Qt,Qwidget,Translucency,我想使用QVideoWidget或QGraphicsVideoItem在具有Qt::FramelessWindowHint标志和Qt::WA_半透明背景图案的QWidget上播放电影。但视频不可见。我只听到声音。有什么问题 编辑: #包括“videoplayer.h” #包括 #包括“qboxlayout.h” #包括“qvideowidget.h” int main(int argc,char*argv[]) { QApplication应用程序(argc、argv); QWidget*w=新

我想使用QVideoWidget或QGraphicsVideoItem在具有Qt::FramelessWindowHint标志和Qt::WA_半透明背景图案的QWidget上播放电影。但视频不可见。我只听到声音。有什么问题

编辑:

#包括“videoplayer.h”
#包括
#包括“qboxlayout.h”
#包括“qvideowidget.h”
int main(int argc,char*argv[])
{
QApplication应用程序(argc、argv);
QWidget*w=新的QWidget;
w->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
w->setAttribute(Qt::WA_半透明背景,true);
w->设置最小尺寸(300200);
QVideoWidget*videoWidget=新的QVideoWidget;
QBoxLayout*controlLayout=新的QHBoxLayout;
controlLayout->setMargin(0);
controlLayout->addWidget(videoWidget);
w->setLayout(控制布局);
QMediaPlayer媒体播放器;
mediaPlayer.setVideoOutput(videoWidget);
setMedia(QUrl::fromLocalFile(“C:/1.wmv”);
videoWidget->show();
mediaPlayer.play();
w->show();
返回app.exec();
}

我不久前已经实现了VideoWidget。唯一需要更改的是视频路径和设置无框架SwindowHint标志


您可以找到源代码。

我不久前已经实现了VideoWidget。唯一需要更改的是视频路径和设置无框架SwindowHint标志


你可以找到源代码。

我解决了这个问题。当我们设置WA_transparcentbackground标志和FramelessWindowHint属性时,QVideoWidget的QPainter将进入QPainter::CompositionMode_DestinationOver模式,导致屏幕上没有显示任何内容或出现阴影。在这种情况下,我使用一个自定义视频小部件,并在创建QPainter painter(此)后在paintEvent中使用;加


更改合成模式。

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


更改合成模式。

请粘贴您的代码好吗?问题是编辑和添加代码,我有问题。当两行w->setWindowFlags(Qt::Window | Qt::FramelessWindowHint)时;w->setAttribute(Qt::WA_半透明背景,true);正确删除电影放映。你能粘贴你的代码吗?问题是编辑和添加代码,我有问题。当两行w->setWindowFlags(Qt::Window | Qt::FramelessWindowHint)时;w->setAttribute(Qt::WA_半透明背景,true);正确删除电影放映。感谢您的回答。只有当直接显示VideoWidget类时,您的解决方案才是正确的。当它作为小部件添加到布局中时,视频再次不可见。感谢您的回答。只有当直接显示VideoWidget类时,您的解决方案才是正确的。当它作为小部件添加到布局中时,视频再次不可见。
#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();
}
painter.setCompositionMode(QPainter::RasterOp_SourceAndNotDestination); or
painter.setCompositionMode(QPainter::RasterOp_SourceAndDestination);