顶级窗口的非像素化圆角 [英] Non-pixelized rounded corner for top-level window

查看:77
本文介绍了顶级窗口的非像素化圆角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 QDialog 上设置圆角.由于它是一个顶级窗口,border-radius 不起作用,所以我必须这样做:

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;
}

我这样称呼它:

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 ?

推荐答案

参加这个聚会有点晚,但也许它会帮助其他人.这显示了如何通过在新的 QBitmap 上绘制来创建像素化程度较低的蒙版(它仍然没有真正抗锯齿,因为位图只有 2 种颜色,但曲线比直接使用 QPainterPath 平滑得多).

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

就我而言,我想掩盖放置在主窗口内的小部件形状(作为中央小部件).4 个边缘周围有 4 个工具栏,我希望中心视图具有圆形边框,并让主窗口背景显示出来.正如 Harald 所建议的那样,这无法通过 CSS 实现,因为小部件的内容实际上并未剪辑到圆形边框.

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.

// MainView is simply a QWidget subclass.
void MainView::resizeEvent(QResizeEvent *event)
{
    QBitmap bmp(size());
    bmp.clear();
    QPainter painter(&bmp);
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setPen(QColor(Qt::black));
    painter.setBrush(QColor(Qt::black));
    painter.drawRoundedRect(geometry(), 20.0f, 20.0f, Qt::AbsoluteSize);
    setMask(bmp);
}

它在 resizeEvent 中,因为它需要知道当前的小部件大小(使用 size()geometry()).这是原始帖子的较短替代方案(我认为),但圆角确实会像素化.

It's in resizeEvent because it needs to know the current widget size (using size() and geometry()). Here's a shorter alternative of the original post (I think), but the rounded edges do get pixelated.

void MainView::resizeEvent(QResizeEvent *event)
{
    QPainterPath path;
    path.addRoundedRect(geometry(), 20.0f, 20.0f, Qt::AbsoluteSize);
    QRegion region = QRegion(path.toFillPolygon().toPolygon());
    setMask(region);
}

这篇关于顶级窗口的非像素化圆角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆