【问题标题】:How to show QWidget created from a QQuickView in frameless dialog with transparent background如何在具有透明背景的无框对话框中显示从 QQuickView 创建的 QWidget
【发布时间】:2014-06-12 11:43:47
【问题描述】:

我有一个QWidget 使用QWidget::createWindowContainerQQuickView 创建:

QQuickView view;
view.setSource(QUrl("./Test.qml"));

QWidget *container = QWidget::createWindowContainer(&view);

Qml 非常简单:

import QtQuick 2.0
Rectangle {
    width: 50
    height: 50
    color: "blue"
    radius: 5
}

我需要一个半透明的无框容器来显示矩形。尝试在 QDialog 中显示小部件:

QDialog dialog;
QHBoxLayout layout;

dialog.setWindowFlags(Qt::FramelessWindowHint);
dialog.setAttribute(Qt::WA_TranslucentBackground);
layout.addWidget(container);
dialog.setLayout(&layout);
dialog.show();

我什么也没得到,因为似乎透明度也传播到了container 对象。如果我尝试添加到layout 而不是container 小部件,例如QPushButton,一切正常,只显示按钮。我的 QML 小部件需要相同的结果。问题可能出在哪里?我正在使用 Qt 5.2.1

【问题讨论】:

    标签: qt transparency qml


    【解决方案1】:

    我怀疑它是否可以用于嵌入在 QWidget 中的 QQuickView。但是你可以有一个透明的 QQuickView 像:

    QQuickView view;
    
    view.setSurfaceType(QSurface::OpenGLSurface);
    
    QSurfaceFormat format;
    format.setAlphaBufferSize(8);
    format.setRenderableType(QSurfaceFormat::OpenGL);
    
    view.setFormat(format);
    view.setColor(QColor(Qt::transparent));
    view.setClearBeforeRendering(true);
    
    view.setFlags(Qt::FramelessWindowHint);
    
    view.setSource(QStringLiteral("./Test.qml"));
    
    view.show();
    

    【讨论】:

    • 谢谢,它有效。但是你认为我可以避免使用 OpenGL 吗?
    猜你喜欢
    • 2017-04-30
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    • 2014-12-13
    • 2018-08-09
    • 2022-01-23
    • 2023-03-08
    相关资源
    最近更新 更多