July 22, 2012

zodiac zodiac
Lab Rat
5 posts

[qt5] Transparent (windows 7 aero) widget

Page  
1

Hello.
How I can set transparent attribute to widget in qt5?
For this action in Qt4 I do:

  1.         hr = pDwmExtendFrameIntoClientArea((HWND)widget->winId(), &m);
  2.         if (SUCCEEDED(hr)) {
  3.             result = true;
  4.             windowNotifier()->addWidget(widget);
  5.         }
  6.  
  7.         widget->setAttribute(Qt::WA_TranslucentBackground, result);

but this doesn’t work in qt5.

Or maybe how I can make aero window the other way in qt5?

16 replies

July 30, 2012

francomartins francomartins
Lab Rat
66 posts

setWindowOpacity an alternative would be to make transparent

December 7, 2012

danut.iurascu danut.iurascu
Lab Rat
17 posts

Does it work using setOpacity for you? I have tried to use it but as an effect the entire window (including all the children) gets transparent, and not only the background.

for eg: using Qt 4.x
it was easy, by using setAttribute(Qt::WA_TranslucentBackground); and viewer.setStyleSheet(“background:transparent;”);

  1. #include <QtGui/QApplication>
  2. #include "qmlapplicationviewer.h"
  3. #include <QtDeclarative>
  4. #include <QDeclarativeContext>
  5.    
  6. Q_DECL_EXPORT int main(int argc, char *argv[])
  7. {
  8.     QScopedPointer<QApplication> app(createApplication(argc, argv));
  9.        
  10.     QmlApplicationViewer viewer;
  11.     viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
  12.     viewer.setMainQmlFile(QLatin1String("qml/main.qml"));
  13.     viewer.setWindowFlags(Qt::FramelessWindowHint);
  14.     viewer.setAttribute(Qt::WA_TranslucentBackground);
  15.     viewer.setStyleSheet("background:transparent;");
  16.     viewer.setResizeMode(QDeclarativeView::SizeViewToRootObject);
  17.     viewer.showExpanded();
  18.      
  19.     return app->exec();
  20. }

But I have no clue how to port the previous code to Qt5, and keeping the transparent background?

any working solution here?

 Signature 

regards,
Dănuț

December 14, 2012

billouparis billouparis
Lab Rat
143 posts

Any progress on this topic?
Thank you,
Regards,
Bill

December 14, 2012

danut.iurascu danut.iurascu
Lab Rat
17 posts

nope!!
:(

I have tried different approaches. 1. Using a QWidget as a top level, but I am not able to load QML content inside it. 2. usind setOpacity(0.0) but this does not work (makes the entire window- also all the children) 3.using OpenGL (but I have not succeed -> using OpenGL as surface type and overwrite the paint method in QQuickView class )

I am out of ideas.
the only thing which gives me hope is that “I am not alone” !

 Signature 

regards,
Dănuț

December 14, 2012

billouparis billouparis
Lab Rat
143 posts

Overwrite the drawBackground method and try this code:

  1. void QmlApplicationViewer::drawBackground(QPainter* painter, const QRectF&)
  2. {
  3.    painter->beginNativePainting();
  4. #ifdef __arm__
  5.    glClearDepthf(1.f);
  6. #else
  7.    glClearDepth(1);
  8. #endif
  9.    glClearColor(0.f, 0.f, 0.f, 0.0f);
  10.    glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
  11.  
  12.    painter->endNativePainting();
  13. }

Let me know if this works,
thank you!
Bill

December 14, 2012

billouparis billouparis
Lab Rat
143 posts

Ooops, apparently that drawBackground method does not exist any longer apparently…
Bill

December 16, 2012

billouparis billouparis
Lab Rat
143 posts

Ok apparently my code is OK

  1. #include <QtGui/QGuiApplication>
  2. #include "qtquick2applicationviewer.h"
  3. #include <QSurface>
  4. #include <QSurfaceFormat>
  5. #include <QDebug>
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9.     QGuiApplication app(argc, argv);
  10.  
  11.     QtQuick2ApplicationViewer viewer;
  12.  
  13.     viewer.setSurfaceType(QSurface::OpenGLSurface);
  14.  
  15.     QSurfaceFormat format;
  16.     format.setAlphaBufferSize(8);
  17.     format.setRenderableType(QSurfaceFormat::OpenGL);
  18.     qDebug() << format.hasAlpha();
  19.     viewer.setFormat(format);
  20.  
  21.     QColor color;
  22.     color.setRedF(0.0);
  23.     color.setGreenF(0.0);
  24.     color.setBlueF(0.0);
  25.     color.setAlphaF(0.0);
  26.     viewer.setColor(color);
  27.  
  28.     viewer.setClearBeforeRendering(true);
  29.  
  30.     //viewer.setFlags(Qt::FramelessWindowHint);
  31.     viewer.setMainQmlFile(QStringLiteral("qml/myProject/main.qml"));
  32.     viewer.showExpanded();
  33.  
  34.  
  35.     return app.exec();
  36. }

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

December 16, 2012

Itehnological Itehnological
Lab Rat
21 posts

The solution is perfect! +1 from me!
P.S. Thanks a lot!

December 16, 2012

billouparis billouparis
Lab Rat
143 posts

Found something in the end.
First activate the aero feature, second use my test application (source code: http://dl.free.fr/nZHhZVRui),
finally you will get something that will look like this:

!http://i48.tinypic.com/35bcxmh.png()!

Enjoy,
Bill

December 24, 2012

Itehnological Itehnological
Lab Rat
21 posts

There is a problem with your solution in Windows, when setting:

  1. format.setRenderableType(QSurfaceFormat::OpenGL);

Which results in the output:

  1. Cant find EGLConfig, returning null config
  2. QQuickWindow: makeCurrent() failed...
  3. ASSERT: "context" in file opengl\qopenglfunctions.cpp, line 194

December 29, 2012

elpuri elpuri
Lab Rat
13 posts

You get that error, because you’re asking for an OpenGL surface and your Qt is configured to use ANGLE which provides an OpenGL ES2 interface using DirectX. If you change the parameter to QSurfaceFormat::OpenGLES it’ll make the error go away, but I doubt it will make the transparency work as a QQuickWindow is always OpenGL anyway.

December 29, 2012

Itehnological Itehnological
Lab Rat
21 posts

@billouparis How did you achieved transparency in windows having in mind what was said by elpuri?

January 11, 2013

billouparis billouparis
Lab Rat
143 posts

I must have a build with openGL flag for sure! I compiled Qt libraries myself!

February 11, 2013

diorahman diorahman
Lab Rat
28 posts

Hi @billouparis how to build the Qt Lib, without ANGLE? Thanks!

April 7, 2013

zhaoweiwei zhaoweiwei
Lab Rat
4 posts

Who know why QMainWindow are completely invisible whenever TranslucentBackground is set?
as fallow:

  1. int main(int argc, char *argv[])
  2. {
  3.     QApplication::setStyle("cleanlooks");
  4.     QApplication a(argc, argv);
  5.     login w;    // class login : public QMainWindow
  6.     w.setWindowTitle("ClientLogin");
  7.  
  8.     w.setWindowOpacity(1);
  9.     w.setWindowFlags(Qt::FramelessWindowHint);
  10. #ifndef NEWQT    //I define this micro for Qt5, because in qt5 WA_TranslucentBackground will cause window invisible
  11.     w.setAttribute(Qt::WA_TranslucentBackground);
  12. #endif
  13.  
  14.     w.show();
  15.     w.move(200,100);
  16.     return a.exec();
  17. }

Page  
1

  ‹‹ issues with QSettings::value()::toInt()      How to swap the value from two lineEdit fields ››

You must log in to post a reply. Not a member yet? Register here!