如何在 Qt 的弹出窗口小部件上创建平滑的圆角 [英] How to create smooth rounded corners on a popup widget in Qt

查看:83
本文介绍了如何在 Qt 的弹出窗口小部件上创建平滑的圆角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了几个小时 - 运气不佳.

I've been searching for hours now - without much luck.

我有一个设置了 windowsflag Qt::Popup 的小部件,我正在尝试创建平滑的圆角.

I have a widget with the windowsflag Qt::Popup set, and I'm trying to create smooth rounded corners.

我尝试过使用样式表,但是角落的透明部分变成了黑色.如果覆盖小部件的绘制事件并在小部件中绘制一个带圆角的矩形,也会发生同样的情况.我也试过设置蒙版,但结果非常像素化.

I've tried using a stylesheet, but then the transparent part of the corners become black. The same happens if a overwrite the widget's paint event and draw a rectangle with rounded corners in the widget. I've also tried to set a mask, but the result gets very pixelated.

经过一些阅读,我发现出现黑角是因为小部件是顶级小部件.但我认为它仍然有可能以某种方式?

After some reading I found out that the black corners appears because the widget is a top-level widget. But I would think that it's still possible somehow?

有谁知道我能做些什么来去除黑角或平滑面膜?任何想法表示赞赏!

Does anyone know what I can do to either get rid of the black corners or to smoothen the mask? Any ideas are appreciated!

绘画事件:

void PopUp::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    QColor greyColor(0xFFC5C6C6);
    QRect rect(0, 0, width(), height());  
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setBrush(QBrush(greyColor));
    painter.setPen(QPen(greyColor));
    painter.drawRoundedRect(rect, 10, 10);
}

设置掩码的函数:

void PopUp::setRoundedCorners(int radius)
{
    QRegion verticalRegion(0, radius, width(), height() - 2 * radius);
    QRegion horizontalRegion(radius, 0, width() - 2 * radius, height());
    QRegion circle(0, 0, 2 * radius, 2 * radius, QRegion::Ellipse);

    QRegion region = verticalRegion.united(horizontalRegion);
    region = region.united(circle);
    region = region.united(circle.translated(width() - 2 * radius, 0));
    region = region.united(circle.translated(width() - 2 * radius, height() - 2 * radius));
    region = region.united(circle.translated(0, height() - 2 * radius));

    setMask(region);
}

推荐答案

  1. 使用Qt::Window 创建小部件|Qt::FramelessWindowHintQt::WA_TranslucentBackground 标志
  2. 在小部件内创建一个 QFrame
  3. 设置一个样式表为QFrame,例如:

边框:1px纯红色;

边框半径:20px;

背景颜色:黑色;

这篇关于如何在 Qt 的弹出窗口小部件上创建平滑的圆角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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