c++qtqgraphicsviewqgraphicssceneqpainterpath

QPainterPath union of shapes (single stroke)


I'm trying to create the union of two shapes with QPainterPath to draw a comic balloon:

const int kb = 4;
QRectF br = text_->boundingRect().adjusted(-kb, -kb, kb, kb);

// anchor on bottom side
qreal y = br.bottom();
qreal x = 0.5 * (br.left() - br.right()) + br.right();
const int kw = 6;

QPainterPath pTip;
pTip.moveTo(offset_);
pTip.lineTo(x - kw, y);
pTip.lineTo(x + kw, y);
pTip.lineTo(offset_);

QPainterPath pRect;
pRect.addRoundedRect(br, 2 * kb, 2 * kb);

shape_->setPath(pTip.united(pRect));

this is what I get:

actual result

whereas I would like to obtain a single shape, with only one continuous outline, like this:

desired result

How can I solve this?


Solution

  • You can use QPainterPath::simplified() to remove the interior edges:

    Returns a simplified version of this path. This implies merging all subpaths that intersect, and returning a path containing no intersecting edges. [...]

    Note that this can mess up Bezier curves if you have them in your path, and that it resets the fill rule. However, because you're not using these features (at least not in the example you have), simplified() should suffice.