How to play a video on a translucent QWidget?

怎甘沉沦 提交于 2019-12-24 00:45:22

问题


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?

Edit:

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

回答1:


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);

to change the composition mode.




回答2:


I've implemented VideoWidget some time ago. The only thing you shall change is your video path and set FramelessWindowHint flag.

You can find source here.



来源:https://stackoverflow.com/questions/23785150/how-to-play-a-video-on-a-translucent-qwidget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!