如何使用 QGraphicsPolygonItem 获得圆帽?

How to get round caps with QGraphicsPolygonItem?

我有这样的代码:

#include <QApplication>
#include <QGraphicsPolygonItem>
#include <QGraphicsScene>
#include <QGraphicsView>

int main(int argc, char ** argv)
{
    QApplication app(argc, argv);
    
    QGraphicsScene scene;
    QGraphicsView view;

    view.resize(640, 400);
    view.setScene(&scene);

    auto polygon = new QGraphicsPolygonItem;
    auto brush = QBrush(QColor(255, 0, 0));
    polygon->setBrush(brush);
    auto pen = QPen(brush, 20);
    pen.setCapStyle(Qt::RoundCap);
    polygon->setPen(pen);

    QPolygonF polygonPath;
    polygonPath << QPointF{-50, -50};
    polygonPath << QPointF{100, -50};
    polygonPath << QPointF{100, 100};
    polygonPath << QPointF{-50, 100};

    polygon->setPolygon(polygonPath);

    scene.addItem(polygon);

    view.show();

    return app.exec();
}

尽管我设置了圆帽,但多边形项目使用直帽渲染。

我是不是做错了什么?

不完全确定,但为了在使用多边形路径时获得您正在寻找的结果,我认为实际上需要设置 'join' 样式而不是 cap 样式。所以改变...

pen.setCapStyle(Qt::RoundCap);

到...

pen.setJoinStyle(Qt::RoundJoin);