tags:

views:

1582

answers:

3

How can i create a partially transparent window with rounded borders (no standard borders)?

(i used Qt::FramelessWindowHint to disable standard borders)

I tried stylesheets, but "border-radius" and "opacity" doesnt seem to have any effect on the window, it only works on children of the enclosing widget.

My second idea was, to make the window fully transparent (with setWindowOpacity), and then add an additional widget with rounded corners (since border-radius works on the children), and then group all my other widgets into that widget, but that does not work since "setWindowOpacity" affects all children aswell (i havent found a way to change this behaviour).

And any ways to make the outer window transparent i could think of (like stylesheets "opacity") dont work properly (i only get a black box instead of a transparent window)

Any help would be highly appreciated.

+5  A: 

Hi,

I think you should use a widget masks, as seen in the following example from Qt :

http://doc.trolltech.com/4.3/widgets-shapedclock.html

I think you'll find what you're looking for in it !

Hope this helps a bit!

Andy M
Yeah, that could be a solution, however it isnt that easy to create a rectangular mask with rounded borders.Well i could use QPixmaps to get such an QRegion, but would the application still be scalable afterwards ?
smerlin
got it working quite well:[code] QPixmap qpm(":/alphamask.png"); qpm = qpm.scaled(this->width(),this->height(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation); if (!qpm.isNull()) this->setMask(qpm.mask()); this->setWindowOpacity(0.9);[/code]
smerlin
Wicked ! Nice job !
Andy M
Helloi tried to search the web for examples on how should the alpha mask image should look like , but i didn't found any examples. say i have the Gimp image editor .what should be in the alpha image map so it could work with the QPixmap ? ( i have no idea on images and graphics )
+3  A: 

I had a similar problem where I wanted to paint on a toplevel widget and have only the painted part appear. setWindowOpacity changed the opacity of the painted part, which I didn't want.

this->setAttribute(Qt::WA_TranslucentBackground, true);

changed the opacity of the widget without the painted part. I just tried tossing on a button, and that also displays perfectly opaque. So you should be able to display other children however you like.

ctd
Yeah, thanks, works like a charm
smerlin
A: 
 void MainForm::resizeEvent(QResizeEvent * /* event */)
{
    QImage image(this->size(), QImage::Format_Mono);
    image.fill(0);

    if(!this->isFullScreen() && !this->isMaximized())
    {
        image.setPixel(0, 0, 1); image.setPixel(1, 0, 1); image.setPixel(2, 0, 1); image.setPixel(3, 0, 1);
        image.setPixel(0, 1, 1); image.setPixel(1, 1, 1);
        image.setPixel(0, 2, 1);
        image.setPixel(0, 3, 1);

        image.setPixel(width() - 4, 0, 1); image.setPixel(width() - 3, 0, 1); image.setPixel(width() - 2, 0, 1); image.setPixel(width() - 1, 0, 1);
                                                                              image.setPixel(width() - 2, 1, 1); image.setPixel(width() - 1, 1, 1);
                                                                                                                 image.setPixel(width() - 1, 2, 1);
                                                                                                                 image.setPixel(width() - 1, 3, 1);

        image.setPixel(0, height() - 4, 1);
        image.setPixel(0, height() - 3, 1);
        image.setPixel(0, height() - 2, 1); image.setPixel(1, height() - 2, 1);
        image.setPixel(0, height() - 1, 1); image.setPixel(1, height() - 1, 1); image.setPixel(2, height() - 1, 1); image.setPixel(3, height() - 1, 1);

                                                                                                                                                  image.setPixel(width() - 1, height() - 4, 1);
                                                                                                                                                  image.setPixel(width() - 1, height() - 3, 1);
                                                                                                    image.setPixel(width() - 2, height() - 2, 1); image.setPixel(width() - 1, height() - 2, 1);
        image.setPixel(width() - 4, height() - 1, 1); image.setPixel(width() - 3, height() - 1, 1); image.setPixel(width() - 2, height() - 1, 1); image.setPixel(width() - 1, height() - 1, 1);
    }
    this->setMask(QPixmap::fromImage(image));
}
Voland