【问题标题】:How to play a video on a translucent QWidget?如何在半透明的 QWidget 上播放视频?
【发布时间】:2014-05-21 13:48:00
【问题描述】:

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

编辑:

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

【问题讨论】:

  • 你能粘贴你的代码吗?
  • 问题是编辑我有问题的代码。当两行 w->setWindowFlags(Qt :: Window | Qt::FramelessWindowHint ); w->setAttribute(Qt::WA_TranslucentBackground, true);正确删除电影节目。

标签: qt qwidget translucency


【解决方案1】:

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

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

改变合成模式。

【讨论】:

    【解决方案2】:

    我前段时间实现了 VideoWidget。您唯一需要更改的是视频路径并设置 FramelessWindowHint 标志。

    你可以找到来源here。

    【讨论】:

    • 感谢您的回答。仅当直接显示 VideoWidget 类时,您的解决方案才是正确的。当它作为小部件添加到布局中时,视频再次不可见。
    猜你喜欢
    • 1970-01-01
    • 2020-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-10
    • 1970-01-01
    相关资源
    最近更新 更多