View Full Version : Toplevel widget with rounded corners.
munna
1st August 2006, 13:32
Hi,
I have a QWidget (frameless) as my mainwindow. I want it to have rounded corners. Is there a way other than setMask() to make the corners rounded ?
Thanks.
zlatko
1st August 2006, 15:00
What make you think that setMask() is bad way ?
munna
1st August 2006, 15:05
I did not say it is a bad way, I was just hoping that there might be a better and simpler way to do this.
Assuming that I use the setMask way, can you please give me a sample where i don't have to use an external image in order to set the mask.
Thanks a lot.
jpn
1st August 2006, 15:09
Assuming that I use the setMask way, can you please give me a sample where i don't have to use an external image in order to set the mask.
Notice that QWidget has 2 overloads of setMask(). One takes QBitmap and another takes QRegion. A QRegion's RegionType can be ellipse. A large enough ellipse clips corners of a rectangle and gives a rounded rectangle.. ;)
munna
1st August 2006, 15:32
Thanks for replying
A QRegion's RegionType can be ellipse. A large enough ellipse clips corners of a rectangle and gives a rounded rectangle
I see your point but the problem is that I am having a square widget. This will always give a circular widget and not rounded corners.
Is it possible to subtract four small circles from the rectangle of the widget and then set the mask ?
jacek
1st August 2006, 15:45
Is it possible to subtract four small circles from the rectangle of the widget and then set the mask ?
QRegion::subtract()
jpn
1st August 2006, 20:53
Is it possible to subtract four small circles from the rectangle of the widget and then set the mask ?
You cannot substract the circles, you will end up having holes in the corners.
This should help unless you already solved the prob ;)
static QRegion roundedRect(const QRect& rect, int r)
{
QRegion region;
// middle and borders
region += rect.adjusted(r, 0, -r, 0);
region += rect.adjusted(0, r, 0, -r);
// top left
QRect corner(rect.topLeft(), QSize(r*2, r*2));
region += QRegion(corner, QRegion::Ellipse);
// top right
corner.moveTopRight(rect.topRight());
region += QRegion(corner, QRegion::Ellipse);
// bottom left
corner.moveBottomLeft(rect.bottomLeft());
region += QRegion(corner, QRegion::Ellipse);
// bottom right
corner.moveBottomRight(rect.bottomRight());
region += QRegion(corner, QRegion::Ellipse);
return region;
}
someWidget->setMask(roundedRect(someWidget->rect(), 20));
jpn
18th August 2006, 20:46
Or yet better, I think:
void RoundedWidget::resizeEvent(QResizeEvent* event)
{
QPixmap pixmap(size());
QPainter painter(&pixmap);
painter.fillRect(pixmap.rect(), Qt::white);
painter.setBrush(Qt::black);
painter.drawRoundRect(pixmap.rect());
setMask(pixmap.createMaskFromColor(Qt::white));
}
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.