如何在QGraphicsScene上绘制填充多边形

时间:2017-04-16 02:08:23

标签: c++ qt qt5 qgraphicsscene qcolor

我想在QGraphicsScene上绘制一个填充了特定颜色的多边形。然后我使用以下代码:

QPolygonF poly;
poly << QPointF(10, 10) << QPointF(10, 50) << QPointF(30, 70 )<< QPointF(60, 50) << QPointF(50, 10);
QBrush brush;
brush.setColor(Qt::red);
QPen pen(Qt::green);
QGraphicsScene graphics_scene_ = new QGraphicsScene(0,0,200,200);
graphics_scene_->addPolygon(poly, pen, brush);
setScene(graphics_scene_);

但是我只得到一个带绿色边框的空心多边形,但多边形内没有红色填充。 我该如何解决?

1 个答案:

答案 0 :(得分:1)

QBrush()缺少样式。你应该使用:

QBrush brush
brush.setColor(Qt::red);
brush.setStyle(Qt::SolidPattern);

或者更好的方式,因为Qt::SolidPattern样式默认出现:

QBrush brush(Qt::red);