Qt: Erase background (Windows Aero Glass)
Asked Answered
O

2

20

Update

see Using Blur Behind on Windows for an example of using Qt and DWM.alt text http://labs.trolltech.com/blogs/wp-content/uploads/2009/09/blurbehind2.png


Original question:

I want to create a Windows Aero Glass window with Qt, now it looks like this: alt text

But after calling some my_window->repaint() my window's label becomes broken: alt text

But now if I resize the window slightly, it repaints properly.


The question is: how do I erase the window background, so that widgets would paint themselves on a clean glass?


The short code to reproduce the problem is (Vista with Aero):

class Window(QWidget):
    def __init__(self, *args):
        QWidget.__init__(self, *args)
        self.setLayout(QVBoxLayout())
        self.layout().addWidget(QLabel("This is the text"))

        # let the whole window be a glass
        self.setAttribute(Qt.WA_NoSystemBackground)
        from ctypes import windll, c_int, byref
        windll.dwmapi.DwmExtendFrameIntoClientArea(c_int(self.winId()), byref(c_int(-1)))
    def mousePressEvent(self, event):
        self.repaint()

You can click the window now, or just hit Alt-Tab several times.

Anyway, using labels with Aero Glass is not what I need, because QLabel doesn't know how to paint itself with a while glow (like the title of the window). What I need is a general way to clean the "glass".

Otten answered 17/1, 2009 at 9:36 Comment(2)
Why call repaint? Can't the window manager just deal with the control drawing on its own?Keithakeithley
Actually you shouldn't repaint — just use Alt-Tab several times, the window will be repainted.Otten
M
6

Just use:

QPainter p

p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
p.fillRect(boundsRect, QColor(0, 0, 0, 0));

This discards the old contents and fills with transparent color.

More info at

Edit: Better use CompositionMode_Clear and paint the rect with whatever color.

Multifarious answered 27/1, 2009 at 19:40 Comment(0)
K
1

I've been googling for a while so I thought I would share the solution:

replace WA_NoSystemBackground to WA_TranslucentBackground and forget about the mousepressevent

now the window is transparent aero glass and re-rendered automatically when needed, yay :)

Kezer answered 16/8, 2011 at 10:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.