QtQuick 2 透明窗口背景 [英] QtQuick 2 Transparent Window Background

查看:53
本文介绍了QtQuick 2 透明窗口背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找如何使我的 QtQuick 2.0 应用程序的背景透明.我发现的大多数答案都使用 QtDeclarative,这适用于 QtQuick 1.0 但不适用于版本 2.

I've been searching how to make the background of my QtQuick 2.0 application transparent. Most answers I've found use QtDeclarative which is ok for QtQuick 1.0 but not version 2.

最后我找到了一个我会发布的答案,但我想知道是否有更好/更简单/更小的方法来完成这项任务.

Finally I found an answer that I will post but I would like to know if there is a better/simpler/smaller way to achieve this task.

注意*

我想让窗口的背景透明.有些人建议 setOpacity 但这会使所有 qml 元素透明.

I wanna make the Background of the window transparent. Some people suggest to setOpacity but this makes all the qml elements transparent.

推荐答案

我在这篇文章中找到了解决方案 http://qt-project.org/forums/viewthread/18984/#106629 来自 billouparis.他使用 QtCreator 生成的主应用程序模板,非常方便.注意:我对原始代码进行了一些更改以使其更小.

I found a solution in this post http://qt-project.org/forums/viewthread/18984/#106629 by billouparis. He uses the main application template that is being generated by QtCreator which is pretty convenient. Note: I changed a little bit the original code to make it smaller.

#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include <QSurface>
#include <QSurfaceFormat>
#include <QDebug>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QtQuick2ApplicationViewer viewer;

    // Make Background Transparent Start
    viewer.setSurfaceType(QSurface::OpenGLSurface);

    QSurfaceFormat format;
    format.setAlphaBufferSize(8);
    format.setRenderableType(QSurfaceFormat::OpenGL);

    viewer.setFormat(format);
    viewer.setColor(QColor(Qt::transparent));
    viewer.setClearBeforeRendering(true);
    // Make Background Transparent Stop

    viewer.setMainQmlFile(QStringLiteral("qml/myProject/main.qml"));
    viewer.showExpanded();
    return app.exec();
}

还要确保根 qml 元素具有 alpha 颜色 (Qt.rgba)

Also make sure that the root qml element has an alpha color (Qt.rgba)

这篇关于QtQuick 2 透明窗口背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆