包含像素图的 QLabel 的圆角

Rounded corners of a QLabel containing a pixmap

我正在尝试在 QT 表单上的 QLabel 中显示图像。我需要那个标签只有左上角和右上角是圆的,而底部 2 个保持矩形。

我使用样式表给 border-radius 一个值并且它起作用了。然而,该标签内的图像仍然是矩形的。 (图像的角隐藏了QLabel的圆角)。

四处寻找,我发现为图像(像素图)设置遮罩并在其上绘制 RoundRect 会导致角为圆形。

有效,但它使图像的所有四个角都变成圆形。

有没有办法只把顶部做成圆形?

这就是我将像素图的边缘设为圆形的方法:

QBitmap map(100,100);     //my pixmap is 100x100
map.fill(Qt::color0);
QPainter painter( &map );
painter.setBrush(Qt::color1);
painter.drawRoundRect(0,0,100,100,20,20);
p.setMask(map);
ui->image1->setPixmap(p);

这就是我制作 QLabel 左上角和右上角圆形的方法

QString style = "border: 4px solid; \n";
style += "border-top-left-radius: 20px;\n";
style += "border-top-right-radius: 20px;";
ui->image1->setStyleSheet(style);

你的面具创意还不错。你只需要对蒙版进行一些合成绘图,例如

QPainter painter(&map);
painter.setBrush(Qt::color1);
painter.drawRoundedRect(0, 0, 100, 40, 20, 20);
painter.drawRect(0, 20, 100, 100);
p.setMask(map);