如何在半透明的 QWidget 上播放视频? [英] How to play a video on a translucent QWidget?

查看:237
本文介绍了如何在半透明的 QWidget 上播放视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to play a movie on a QWidget that has Qt::FramelessWindowHint flag and Qt::WA_TranslucentBackground attirbute using QVideoWidget or QGraphicsVideoItem. But the video is not visible. I hear only sound. What is the problem?

#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 标志和 FramelessWindowHint 属性时,QVideoWidget 的 QPainter 进入 QPainter::CompositionMode_DestinationOver 模式,它导致没有显示或屏幕上的阴影.在这种情况下,我在创建 QPainter Painter(this) 后使用自定义视频小部件和paintEvent;添加

I solve the problem .when we set the WA_TranslucentBackground flag and FramelessWindowHint attribute the QVideoWidget's QPainter going to QPainter::CompositionMode_DestinationOver mode and it cause to nothing to show or a shadow on the screen . in this case i use a custom video widget and in paintEvent after createing QPainter painter(this); add

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

更改合成模式.

这篇关于如何在半透明的 QWidget 上播放视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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