c++ - 无法在不破坏堆叠顺序的情况下使 QQuickWidget 背景透明

标签 c++ qt qml qt5 qquickwidget

我在使 QQuickWidget 背景透明时遇到问题。

我想在 QWidget 下面放置一个 QQuickWidgetQQuickWidget 使用源 qml 文件。当我使用 Qt 4.8 时,我使用了 QDeclarativeView。从 Qt 4 移植到 Qt 5,不再使用 QDeclarativeView。因此,我使用QQuickWidget而不是QDeclarativeView,如下所示:

 QWidget *mainWidget = new QWidget();
 mainWidget->setStyleSheet("background-image: url(:/background.png);");

 QQuickWidget *quick = new QQuickWidget(mainWidget);
 quick->setAttribute(Qt::WA_TranslucentBackground, true);
 quick->setAttribute(Qt::WA_AlwaysStackOnTop, true);
 quick->setClearColor(Qt::transparent);
 quick->setSource(QUrl("qrc:/image.qml"));

 QWidget *topWidget = new QWidget(mainWidget);
 topWidget->setStyleSheet("background-image: url(:/semitransparent.png);");

如果我这样做:

setAttribute(Qt::WA_AlwaysStackOnTop, true);

然后背景变得透明,但破坏了同一窗口内 QQuickWidget 下其他小部件的堆叠顺序。

我想让QQuickWidgetQWidget下面时透明。这可能吗?如果没有,您建议采取什么解决方法?

(1)这是mainWidget的背景图片:

enter image description here

(2) 这是QQuickWidget的背景。 qml 文件使用此图像:

enter image description here

(3) 这是topWidget的背景图片:

enter image description here

(4)我想要什么:

enter image description here

(5) 将 WA_AlwaysStackOnTop 设置为 false 时得到的结果:

enter image description here

(6) 将 WA_AlwaysStackOnTop 设置为 true 时得到的结果:

enter image description here

最佳答案

官方 QT 文档在这里 http://doc.qt.io/qt-5/qquickwidget.html表示会破坏堆叠顺序:

When absolutely necessary, this limitation can be overcome by setting the Qt::WA_AlwaysStackOnTop attribute on the QQuickWidget. Be aware, however that this breaks stacking order. For example it will not be possible to have other widgets on top of the QQuickWidget, so it should only be used in situations where a semi-transparent QQuickWidget with other widgets visible underneath is required.

另请参阅此官方博客条目:http://blog.qt.io/blog/2014/07/02/qt-weekly-16-qquickwidget/

最近的这篇博客文章涉及 QT 5.1 中的一项新功能:http://www.ics.com/blog/combining-qt-widgets-and-qml-qwidgetcreatewindowcontainer

结论:您所看到的不是错误,而是 QT 框架已知的、公认的、公开宣传的限制。

我的建议:不要尝试通过 hack 来解决这个问题,而是重新设计你的 UI 方法。例如,也许您可​​以制作 topWidget alpha 混合(半透明)。

编辑:例如,像这样:

enter image description here

关于c++ - 无法在不破坏堆叠顺序的情况下使 QQuickWidget 背景透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41696239/

相关文章:

c++ - QT - 指向 QColor 的指针可以直接访问,但不能

c++ - 具有输出参数的 Qt invokeMethod 调用函数

c++ - 混合 QML/C++ 应用程序在部署后不会显示任何内容

qt-creator - 如何将资源添加到 .qmlproject

c++ - 我可以使用常量模板吗?

c++ - 如何在禁用异常的情况下从 constexpr 函数断言?

c++ - 在 OpenGL 上延迟渲染 Qt

c++ - 如何从路径加载 QImage?

c# - 如何在 C# 中编码 SIZE 结构?

c++ - 带有多行单元格的 QTableView