[solved] Qt5 - how to port a simple QtQuick application to Qt5 and keep the transparent background?



  • I am trying to port a simple Qt Quick application from Qt4.8 to Qt5.0beta.
    my initial (Qt4.8) code is similar to what is below:

    main.cpp will dipsplay a QDeclarativeView in a frameless window with translucent background
    @#include <QtGui/QApplication>
    #include "qmlapplicationviewer.h"
    #include <QtDeclarative>
    #include <QDeclarativeContext>

    Q_DECL_EXPORT int main(int argc, char *argv[])
    {
    QScopedPointer<QApplication> app(createApplication(argc, argv));

    QmlApplicationViewer viewer;
    viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
    viewer.setMainQmlFile&#40;QLatin1String("qml/main.qml"&#41;&#41;;
    
    viewer.setWindowFlags(Qt::FramelessWindowHint&#41;;
    
    viewer.setAttribute(Qt::WA_TranslucentBackground&#41;;
    viewer.setStyleSheet("background:transparent;");
    viewer.setResizeMode(QDeclarativeView::SizeViewToRootObject);
    viewer.showExpanded();
    
    return app->exec&#40;&#41;;
    

    }@

    and the main.qml will just display a red rectangle inside another transparent rectangle.
    @ import QtQuick 1.1

    Rectangle {
    width: 360
    height: 360
    color: "transparent"
    Rectangle
    {
    x: 125
    y: 122
    width: 110
    height: 116
    anchors.centerIn: parent
    color: "red"
    radius: 27
    }

    Text {
     color: "black"
        text: qsTr("Press me!"&#41;
        font.pointSize: 14
        anchors.centerIn: parent
    }
    
    MouseArea {
        anchors.fill: parent
        onClicked: {
            Qt.quit(&#41;;
        }
    }
    

    }@

    In order to make it compatible with Qt5 few changes were necessary.
    (I have followed "http://qt-project.org/doc/qt-5.0/portingqmlapp.html":http://qt-project.org/doc/qt-5.0/portingqmlapp.htmlhttp://
    in order not to miss something)

    Now main.cpp is similar to:
    @#include <QtGui/QGuiApplication>
    #include "qtquick2applicationviewer.h"
    #include <QtWidgets/QtWidgets>
    #include <QtWidgets/QLabel>

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

    QGuiApplication app(argc, argv);
    QtQuick2ApplicationViewer viewer;
    viewer.setMainQmlFile&#40;QStringLiteral("qml/main.qml"&#41;&#41;;
    viewer.setWindowFlags(Qt::FramelessWindowHint&#41;;
    viewer.showExpanded(&#41;;
    
    
    return app.exec&#40;&#41;;
    

    }@

    In main.qml only one line was replaced.
    @import QtQuick 2.0@

    but I am not able to find a way of keeping the transparency

    @setAttribute(Qt::WA_TranslucentBackground);
    setStyleSheet("background:transparent;");@

    setAttribute and setStyleSheet were available for a QDeclarativeView (which was a QWidget)
    but not for QQuickView (which is a QWindow)

    Has anyone any idea how to set the background transparent, for the main window in Qt5?



  • I wish you found a solution for this, I am looking for exactly the same thing.
    I personaly set the surface to openGLSurface,
    and set the format with an alpha Buffer Size of 8, and a renderable type of OpenGL.
    I am assuring to have a ClearBeforeRendering set to true.
    I set a color to black (0,0,0), but it is apparently not sufficient.
    I don't know how to say to the QQuickView to clear with an alpha value set to 0.

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

    QtQuick2ApplicationViewer viewer;
    
    viewer.setSurfaceType(QSurface::OpenGLSurface);
    
    QSurfaceFormat format;
    format.setAlphaBufferSize(8);
    format.setRenderableType(QSurfaceFormat::OpenGL);
    viewer.setFormat(format);
    
    
    viewer.setColor(QColor(0,0,0));
    viewer.setClearBeforeRendering(true);
    
    viewer.setOpacity(0);
    
    viewer.setMainQmlFile&#40;QStringLiteral("qml/someProject/main.qml"&#41;&#41;;
    viewer.showExpanded();
    
    
    return app.exec(&#41;;
    

    }@

    And it is still not working (at least under windows 7, does anybody know I shall proceed, without overloading the paint method of the QtQuick2ApplicationViewer itself?
    Anybody?

    Bill



  • Hi Bill,
    I am out of ideas, please let me know if you manage to find a solution!



  • Ok apparently my code is OK
    @#include <QtGui/QGuiApplication>
    #include "qtquick2applicationviewer.h"
    #include <QSurface>
    #include <QSurfaceFormat>
    #include <QDebug>

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

    QtQuick2ApplicationViewer viewer;
    
    viewer.setSurfaceType(QSurface::OpenGLSurface);
    
    QSurfaceFormat format;
    format.setAlphaBufferSize(8);
    format.setRenderableType(QSurfaceFormat::OpenGL);
    qDebug() <&lt; format.hasAlpha();
    viewer.setFormat(format);
    
    QColor color;
    color.setRedF(0.0);
    color.setGreenF(0.0);
    color.setBlueF(0.0);
    color.setAlphaF(0.0);
    viewer.setColor(color);
    
    viewer.setClearBeforeRendering(true);
    
    //viewer.setFlags(Qt::FramelessWindowHint);
    viewer.setMainQmlFile&#40;QStringLiteral("qml/myProject/main.qml"&#41;);
    viewer.showExpanded();
    
    
    return app.exec();
    

    }
    @

    from my investigation on the Qt5 side in
    void QSGDefaultRenderer::render() and then in
    void QSGBindable::clear(QSGRenderer::ClearMode mode) const,
    the glClear call is performed correctly, but sadly and this is my
    conclusion, Windows is doing something under Qt that can not be directly/easily controlled from within Qt.

    What do you think?
    Bill



  • Hi Bill,

    I wish you a Happy New Year !!

    I was on vacation for a couple of weeks, and I tried to be as offline as possible.
    Your solution is working fine for me (on Fedora).
    viewer.setClearBeforeRendering(true); might be skipped, accordingly with documentation "color buffer is cleared by default".

    thanks a lot for your support!!

    Regarding your open question ( QSGDefaultRenderer::render() and QSGBindable::clear(QSGRenderer::ClearMode mode) )
    glClear is indeed performed, but is this enough or not!?



  • I just re-tested this solution on UBuntu 12.10 and it seems fine to me too.
    I will try to explore the QtGraphicEffects now, and in particular the Mask capabilities.
    Regards,
    Bill



  • Can I conclude that this is not possible under Windows?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.