Mysterious flickering arrow
-
I'm trying to draw a multi-segment line with an arrow at one end. I have a class derived from QGraphicsPathItem that has a QPolygonF member. For the line I'm building the path, then create the points for the arrow based on the last two points of the line segment (to get the rotation and position).
It all works, except that when the arrow polygon is drawn, every other re-draw is not filled. Here's the paint code:
@void NodeConnector::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
{
QGraphicsPathItem::paint(painter, option, widget);
painter->setBrush(Qt::SolidPattern);
painter->drawPolygon(arrow);
}
@Here's how the polygon is set up:
@
void NodeConnector::setArrow(QPointF & from, QPointF & to)
{
static QPolygonF arrowHead;
arrowHead << QPointF(0.0, 0.0) << QPointF(12, 4) << QPointF(12, -4);// << QPointF(0,0);QPointF diff = from - to; double angle = atan2(diff.y(), diff.x()); // This is the angle relative to west-pointing double cosang = cos(angle); double sinang = sin(angle); // // Create temporary, rotate arrow arrow.clear(); for (int i = 0; i < arrowHead.size(); ++i) { qreal x = arrowHead.at(i).x() * cosang - arrowHead.at(i).y() * sinang + to.x(); qreal y = arrowHead.at(i).y() * cosang + arrowHead.at(i).x() * sinang + to.y(); arrow << QPointF(x,y); }
}
@
So far every variation I've tried (changing the class to a QGraphicsItem with a line and polygon, making the arrow a separate QGraphicsItem) makes no difference; every other redraw, the arrow is just an outline. I'm pretty new to Qt so I'm sure I'm just missing something fundamental.Thanks.