我一直在寻找如何制作我的 QtQuick 2.0 申请 transparent 的 background . 我发现的大多数答案都使用QtDeclarative,这对于QtQuick 1.0而言不是第2版 .
最后我找到了一个我要发布的答案,但我想知道是否有更好/更简单/更小的方法来完成这项任务 .
Note*
我想让窗户的背景透明 . 有人建议使用setOpacity,但这会使所有qml元素透明 .
我在billouparis的这篇文章中找到了一个解决方案http://qt-project.org/forums/viewthread/18984/#106629 . 他使用QtCreator生成的主应用程序模板非常方便 . Note: 我改变了原始代码以使其变小 .
#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)
试试这个
import QtQuick 2.2 import QtQuick.Window 2.0 Window { id: backlight visible: true title: qsTr("backlight") width: 500 height: 50 x: (Screen.width - width) / 2 y: (Screen.height - height) / 2 color: "transparent" }
这里是一个在纯qml中获得无框透明窗口的示例
import QtQuick 2.2 import QtQuick.Window 2.0 import QtQuick.Controls 1.1 import QtQuick.Controls.Styles 1.1 ApplicationWindow { id: backlight flags: Qt.FramelessWindowHint visible: true title: qsTr("backlight") width: 500 height: 50 x: (Screen.width - width) / 2 y: (Screen.height - height) / 2 color: "transparent" property real slideValue signal onSlide(real value) Rectangle { anchors.centerIn: parent width: parent.width height: 50 color: "transparent" Rectangle { anchors.fill: parent radius: 25 opacity: 0.3 color: "gray" } Slider { anchors.centerIn: parent width: backlight.width - 16 height: backlight.height value: backlight.slideValue focus: true onValueChanged: backlight.onSlide(value) Keys.onSpacePressed: Qt.quit() Keys.onEscapePressed: Qt.quit() style: SliderStyle { groove: Rectangle { implicitHeight: 8 radius: 4 color: "gray" } handle: Rectangle { anchors.centerIn: parent color: control.pressed ? "white" : "lightgray" border.color: "gray" border.width: 2 width: 34 height: 34 radius: 17 } } } } }
3 回答
我在billouparis的这篇文章中找到了一个解决方案http://qt-project.org/forums/viewthread/18984/#106629 . 他使用QtCreator生成的主应用程序模板非常方便 . Note: 我改变了原始代码以使其变小 .
还要确保根qml元素具有alpha颜色(Qt.rgba)
试试这个
这里是一个在纯qml中获得无框透明窗口的示例