Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt: Erase background (Windows Aero Glass)

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

like image 566
Andrew T Avatar asked Jan 17 '09 09:01

Andrew T


2 Answers

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

  • http://techbase.kde.org/Development/Tutorials/Graphics/Performance#QPixmap::setAlphaChannel.28.29

  • http://doc.qt.digia.com/qtjambi-4.4/html/com/trolltech/qt/gui/QPainter.CompositionMode.html

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

like image 126
Viesturs Avatar answered Nov 09 '22 18:11

Viesturs


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 :)

like image 26
ben Avatar answered Nov 09 '22 20:11

ben