View Full Version : Rounded corners for a QWidget

31st August 2011, 21:49
Hi all
I am new to this great and helpful forum! I solved many problems with solutions here posted. So here is my contribution:

Actually we are programming a login manager for Linux/Unix systems: LINK (http://forum.manjarolinux.org/viewtopic.php?id=104)
We are now in a state, where we are concentrating on the theme management and style. Therefore I needed a function to create a widget with rounded corners. But I wanted to have the ability to give each corner a different radius. I found here some small code examples, but only for adjusting all corners with the same radius. So, I want to fill this gap. Here is my code example, to give a widget rounded corners, with the ability to give each corner a different radius:

void Widget::setRoundedCorners(int radius_tl, int radius_tr, int radius_bl, int radius_br) {
QRegion region(0, 0, width(), height(), QRegion::Rectangle);

// top left
QRegion round (0, 0, 2*radius_tl, 2*radius_tl, QRegion::Ellipse);
QRegion corner(0, 0, radius_tl, radius_tl, QRegion::Rectangle);
region = region.subtracted(corner.subtracted(round));

// top right
round = QRegion(width()-2*radius_tr, 0, 2*radius_tr, 2*radius_tr, QRegion::Ellipse);
corner = QRegion(width()-radius_tr, 0, radius_tr, radius_tr, QRegion::Rectangle);
region = region.subtracted(corner.subtracted(round));

// bottom right
round = QRegion(width()-2*radius_br, height()-2*radius_br, 2*radius_br, 2*radius_br, QRegion::Ellipse);
corner = QRegion(width()-radius_br, height()-radius_br, radius_br, radius_br, QRegion::Rectangle);
region = region.subtracted(corner.subtracted(round));

// bottom left
round = QRegion(0, height()-2*radius_bl, 2*radius_bl, 2*radius_bl, QRegion::Ellipse);
corner = QRegion(0, height()-radius_bl, radius_bl, radius_bl, QRegion::Rectangle);
region = region.subtracted(corner.subtracted(round));


Roland ;)

1st September 2011, 11:11
Well, style sheets do all that for you, and offer you a greater flexibility, because you don't have to implement your style in code, and can therefore Style the same exe any way you like with Stylesheets with out the need to rebuild.
Have a look at border-radius here:

However, the style sheet border-radius is only supported by some widgets.
You solution is good for those not supported.

1st September 2011, 14:51

Supports only the background, background-clip and background-origin properties.

If you subclass from QWidget, you need to provide a paintEvent for your custom QWidget as below....

@high_flyer: Yeah the style sheets is a nice thing, but for a simple QWidget useless.


1st September 2011, 17:40
@high_flyer: Yeah the style sheets is a nice thing, but for a simple QWidget useless.

why should it be useless?

1st September 2011, 17:51
In the sense, that QWidget does not support rounded corners over Stylesheets, as I added in the last section in post #2.

2nd September 2011, 08:46
did I mention I hate edits? ;)

thanks, didn't know that.