【问题标题】:How to get round caps with QGraphicsPolygonItem?如何使用 QGraphicsPolygonItem 获得圆帽?
【发布时间】:2020-12-04 07:41:59
【问题描述】:

我有这样的代码:

#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();
}

尽管我设置了圆帽,但多边形项目会以直帽呈现。

我在这里做错了什么吗?

【问题讨论】:

    标签: c++ qt qt5 qgraphicsscene qgraphicsitem


    【解决方案1】:

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

    pen.setCapStyle(Qt::RoundCap);
    

    到...

    pen.setJoinStyle(Qt::RoundJoin);
    

    【讨论】:

      猜你喜欢
      • 2016-12-05
      • 1970-01-01
      • 1970-01-01
      • 2020-04-25
      • 1970-01-01
      • 1970-01-01
      • 2013-06-06
      • 1970-01-01
      • 2012-09-27
      相关资源
      最近更新 更多