顶级窗口的非像素化圆角
我想在 QDialog 上设置圆角。因为它是一个顶级窗口,所以 border-radius 不起作用,所以我必须这样做:
QRegion EnterPinDialog::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;
}
并且我这样称呼它:
this->setMask(roundedRect(this->rect(), 8));
它可以工作,但问题是角是像素化的。
有没有办法在没有这些像素化角的情况下获得它?如果是,怎么办?
I want to set rounded corners on a QDialog. Since it is a top-level window, border-radius doesn't work, so I've to do this :
QRegion EnterPinDialog::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;
}
and I call it this way :
this->setMask(roundedRect(this->rect(), 8));
It works, but the problem is that corners are pixelized.
Is there a way to get it without having these pixelized corners ? If yes, how ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
参加这个聚会有点晚了,但也许对其他人有帮助。这展示了如何通过在新的 QBitmap 上绘制来创建像素化程度较低的蒙版(它仍然没有真正抗锯齿,因为位图只有 2 种颜色,但曲线比直接使用 QPainterPath 平滑得多)。
就我而言,我想掩盖放置在主窗口内的小部件形状(作为中央小部件)。 4 个边缘周围有 4 个工具栏,我希望中心视图具有圆形边框,并让主窗口背景显示出来。正如 Harald 所建议的那样,这无法通过 CSS 实现,因为小部件的内容实际上并未剪辑到圆形边框。
它位于 resizeEvent 中,因为它需要知道当前小部件的大小(使用
size()
和geometry()
)。这是原始帖子的较短替代方案(我认为),但圆形边缘确实会像素化。Kinda late to this party, but maybe it will help someone else. This shows how to create an less pixelated mask by drawing on top of a new QBitmap (it's still not really antialiased because a bitmap has only 2 colors, but the curve is much smoother than using a QPainterPath directly).
In my case I wanted to mask a widget shape which is placed within a main window (as central widget). There are 4 toolbars around the 4 edges, and I wanted the center view to have rounded borders and let the main window background show through. This was not doable via CSS as Harald suggests since the contents of the widget didn't actually clip to the rounded border.
It's in resizeEvent because it needs to know the current widget size (using
size()
andgeometry()
). Here's a shorter alternative of the original post (I think), but the rounded edges do get pixelated.显示的 setAttribute( Qt.WA_TranslucentBackground, True) 方法 这里也有效...只需在顶级窗口上设置此属性,然后使用 paintEvent() 方法覆盖中的 QPainterPath 绘制窗口的形状。
这里有一些(python)代码,可以帮助您构造或绘制圆角矩形的 QPainterPath。
The setAttribute( Qt.WA_TranslucentBackground, True) method shown here also works... Simply set this attribute on your top-level window, and paint the shape of your window using a QPainterPath in a paintEvent() method override.
Here's some (python) code that might help you construct, or paint, the QPainterPath for a rectangle with rounded corners.
根据您想要的对话框外观,您甚至可以通过 CSS 完全重新设计顶层窗口的样式,它们遵循盒模型,请参阅整个 样式表 文档。
会给你一个 2px 宽、半径为 2px 的边框。
我一般来说你应该能够通过样式表处理大部分 ui 定制需求
Depending on what you want the dialog to look like you can completely restyle even toplevel windows via css, they adhere to the box model see the whole stylesheet documentation.
Will give you a 2px wide border with a radius of 2px.
I general you should be able to handle most of your ui customisation needs through the stylesheets