Qt: Erase background (Windows Aero Glass)

aero-glassdwmpyqtwindows-vista

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".

Best Answer

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.

Related Topic