[Qt5] How to make a QQuickView TranslucentBackground?



  • I want to use setAttribute(Qt::WA_TranslucentBackground), it used to be a method of QDeclarativeView in Qt4.8.3.

    When I replace QDeclarativeView with QQuickView, the setAttribute method disappears..

    I noticed that QQuickView is inherited from QQuickWindow, it seemed not related with QWidget, when the old QDeclarativeView is indirectly inherited from QWidget( setAttribute is a method of QWidget ).

    So in Qt5 how can I implement a TranslucentBackground window that using qml? (Meanwhile I want to use QtQuick 2.0)

    Thank you !



  • this code works for me under Ubuntu 12.10

    @#include <QtGui/QGuiApplication>
    #include "qtquick2applicationviewer.h"
    #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/MaskTest/main.qml"&#41;);
    viewer.showExpanded();
    
    return app.exec();
    

    }
    @

    Hope it helps
    Bill



  • Thank you Bill,
    Follow your code, I write this:

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

    QQuickView view;

    view.setSurfaceType(QSurface::OpenGLSurface);

    QSurfaceFormat format;
    //QSurfaceFormat format = view.format();
    format.setAlphaBufferSize(8);
    format.setRenderableType(QSurfaceFormat::OpenGL);
    qDebug() <&lt; format.hasAlpha();
    view.setFormat(format);
    
    QColor color;
    color.setRedF(0.0);
    color.setGreenF(0.0);
    color.setBlueF(0.0);
    color.setAlphaF(0.0);
    view.setColor(color);
    
    view.setClearBeforeRendering(true);
    
    view.connect(view.engine(), SIGNAL(quit()), SLOT(close()));  
    view.setFlags(Qt::FramelessWindowHint);
    
    view.setSource(QUrl::fromLocalFile&#40;"qml/test.qml"&#41;);
    

    view.show();

    return app->exec();
    }
    @

    the program will crash when executing app->exec(), it said:

    ASSERT: "context" in file opengl\qopenglfunctions.cpp, line 194

    then I pressed alt+esc, it poped another error:

    ASSERT failure in QSGNode::appendChildNode: "QSGNode already has a parent", file scenegraph\coreapi\qsgnode.cpp, line 404

    Comment view.setFormat(format); would make the program running without crash, but it's not transparent. The area should be transparent now is in black color.

    I'm using WinXP.

    The remaining problem is why I can't call setFormat method. Is there anyone facing the same problem?



  • According to this issue, https://bugreports.qt-project.org/browse/QTBUG-28214
    Transparent OpenGL windows are currently not supported on Windows?



  • You might find the beginning of a solution in this topic:
    http://qt-project.org/forums/viewthread/18984/

    In windows XP, the issues might be related to:
    -windows XP window manager itself (as in Windows 7)
    -the way you compiled your qt5, don't forget to use option -opengl if you want to use your video hardware.

    Regards,
    Bill



  • Thank you Bill, I will try to compile Qt5 with opengl and report my results here.



  • I compiled with -opengl desktop. Now using your code, the program will not crash, but still not transparent. The area should be transparent has a black background.

    So it is because transparent OpenGL windows are not supported on Windows?



  • Interesting, what we have now?


Log in to reply
 

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