我正在尝试在我的窗口小部件中创建掠过的 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);
}
上面的代码结果如下:
我想正确填写。
附言
我已经尝试过
simplified
,connectPath
甚至united
的方法,但是都没有用。 最佳答案
当您将子路径连接起来以形成更大的路径时,您需要更加谨慎。特别是,请查看代码中path
,path2
和path3
的起点和终点,然后查看它们的连接顺序。
在这种情况下,当将它们组合成path2
时,您应该能够通过reversing path3
和arch
来纠正问题。
QPainterPath arch;
arch.connectPath(path2.toReversed());
arch.connectPath(path3.toReversed());
arch.connectPath(path);