我正在为我的项目开发一个讲话泡泡。我正在使用无框架QDialog
,其中我以交叉的方式绘制了两个椭圆矩形,如下所示。
我怎样才能删除相交部分?我已在图像中标记了该部分。
我对Qt很新。代码是:
QPainter painter(this);
painter.save();
painter.setRenderHint(QPainter::Antialiasing);
QPen pen(QColor(65, 167, 243), 3,Qt::SolidLine);
QBrush brush(QColor(255, 255, 255,204));
painter.setPen(pen);
painter.setBrush(brush);
QPainterPath rounded_rect;
rounded_rect.addRoundedRect(QRectF(20, 0, 180, 100), 16, 16);
rounded_rect.addRoundedRect(QRectF(0, 50 - 20, 40, 40 ), 16, 16);
painter.setClipPath(rounded_rect);
QRegion maskregion = painter.clipRegion();
setMask(maskregion);
painter.drawPath(rounded_rect);
painter.restore();
答案 0 :(得分:1)
有可能。你应该看一下Qt文档:
修改:您应该将代码更改为以下内容(未经过测试!)
QPainterPath p1, p2;
p1.addRoundedRect(QRectF(20, 0, 180, 100), 16, 16);
p2.addRoundedRect(QRectF(0, 50 - 20, 40, 40 ), 16, 16);
p1 += p2; // or -= if you want to remove that part
QRegion r(p1.toFillPolygon().toPolygon());
setMask(r);
编辑2:另一种解决方案是覆盖paintEvent()
不是创建蒙版,而是可以通过覆盖paintEvent
并绘制任意复杂形式的背景来在透明窗口上绘画。但是,我不确定这是否适用于基于QDialog的Windows。
答案 1 :(得分:0)
我找到了解决方案。谢谢bkausbk.Here是代码:
QPainter painter(this);
painter.save();
painter.setRenderHint(QPainter::Antialiasing);
QPen pen(QColor(65, 167, 243), 3,Qt::SolidLine);
QBrush brush(QColor(255, 255, 255,204));
painter.setPen(pen);
painter.setBrush(brush);
QPainterPath rounded_rect1,rounded_rect2,round;
rounded_rect1.addRoundedRect(QRectF(20, 0, 180, 100), 16, 16);
rounded_rect2.addRoundedRect(QRectF(0, 50 - 20, 40, 40 ), 16, 16);
round = rounded_rect1 + rounded_rect2;
painter.setClipPath(round);
QRegion maskregion = painter.clipRegion();
setMask(maskregion);
painter.drawPath(round);
painter.restore();