如何绑定或合并 QPainterPaths?

How to bind or merge QPainterPaths?

我正在尝试在我的小部件中创建拉丝 arch,以便它占据顶部。我设法画了它,但很难填充它。 你能帮帮我吗?

void Curve::paintEvent(QPaintEvent *) {

     QPainter painter(this);
     painter.setRenderHint(QPainter::Antialiasing);
     painter.setPen(QPen(QColor("#4681c5"), 2.5, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin));  /// #4681c5    

     QPainterPath path;
     path.moveTo(width()/2 + 85.2, 71);
     path.cubicTo(width()/2 + 86.2, 71, width()/2 + 97, 102, width()/2 + 137, 102);

     QPainterPath path2;
     path2.moveTo(width()/2 - 85.2, 71);
     path2.cubicTo(width()/2 - 86.2, 71, width()/2 - 97, 102, width()/2 - 137, 102);

     QPainterPath path3;
     path3.arcMoveTo(width()/2 - 95, 18, 190, 190, 26);
     path3.arcTo(width()/2 - 95, 18, 190, 190, 26, 128);

     QPolygonF leftpoly;
     leftpoly << QPointF(0, 0) << QPointF(0, 102) << QPointF(width()/2 - 137, 102);

     QPolygonF rightpoly;
     rightpoly << QPointF(width()/2 + 137, 102) << QPointF(width(), 102) << QPointF(width(), 0) << QPointF(0, 0);

     QPainterPath arch;
     arch.connectPath(path2);
     arch.connectPath(path3);
     arch.connectPath(path);
     QPainterPath fill;
     fill.addPolygon(leftpoly);
     fill.connectPath(arch);
     fill.addPolygon(rightpoly);
     painter.fillPath(fill, QBrush(QColor("#f68448")));

     path.addPolygon(rightpoly);
     path2.addPolygon(leftpoly);
     path3.addPath(path);
     path3.addPath(path2);
     painter.drawPath(fill);
}

上面代码的结果如下:

我要正确填写

P.S。 simplifiedconnectPath 甚至 united 方法我都试过了,但都没有用。

连接子路径以形成更大的路径时,您需要更加注意子路径的定向方式。特别是,查看代码中 pathpath2path3 的起点和终点,然后查看连接它们的顺序。

在这种情况下,当将 reversing path2path3 组合成 arch...

时,您应该能够更正它们
QPainterPath arch;
arch.connectPath(path2.toReversed());
arch.connectPath(path3.toReversed());
arch.connectPath(path);