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

标签 qt qwidget translucency

我想使用 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();
}

最佳答案

我解决了这个问题。当我们设置 WA_TranslucentBackground 标志和 FramelessWindowHint 属性时,QVideoWidget 的 QPainter 将进入 QPainter::CompositionMode_DestinationOver 模式,这会导致屏幕上不显示任何内容或出现阴影。在本例中,我使用自定义视频小部件,并在创建 QPainter Painter(this) 之后在 PaintEvent 中使用;添加

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

更改合成模式。

关于qt - 如何在半透明的QWidget上播放视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23785150/

相关文章:

c++ - 如何在 QListView 中对图像应用图形效果?

macos - Mac OS 10.9 上的 QMetaObject::changeGuard 崩溃

python - 调整 QWidget 大小时忽略最小尺寸

ios - 导航栏外观 barTintColor alpha 组件

ios - 在 iOS 应用程序中查看主屏幕 (Swift)

qt - 取消所有未完成的 QTimer 事件

c++ - 强制 QByteArray 将传递的 C 样式数组的所有权交给它的构造函数

qt - 如何制作 Qt 交互式文本编辑小部件

css - 使用 Qt 固定位置(与 css 效果相同)

ios - 半透明状态栏 (iPhone/iPad/iPod Touch)