QtQuick 2 Transparent Window Background

后端 未结 3 1693
太阳男子
太阳男子 2021-01-01 06:20

I\'ve been searching how to make the background of my QtQuick 2.0 application transparent. Most answers I\'ve found use Qt

3条回答
  •  隐瞒了意图╮
    2021-01-01 07:04

    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 
    #include "qtquick2applicationviewer.h"
    #include 
    #include 
    #include 
    
    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();
    }
    

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

提交回复
热议问题