I'm building a mapping application based on graphics view framework. Everything works fine except that some lines disappear when dragging. It seems the item to be judged out of the view, but actually just part of it. Below are screenshots.
I subclassed QGraphicsView
and QGraphicsItem
, added items to the scene which is connected to view. To implement the dragging function, I simply use setDragMode()
in subclass's constructor.
And in MapShape.cpp:
QRectF Polyline::boundingRect() const
{
return QRectF(minX-pen.width()/2, minY-pen.width()/2, maxX-minX+pen.width(), maxY-minY+pen.width());
}
void Polyline::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
pen.setCosmetic(true);
painter->setPen(pen);
for(auto iter = points.begin(); iter != points.end()-1; ++iter)
{
painter->drawLine(*iter, *(iter+1));
}
}
Could someone help? Thank you!
The code isn't complete enough to be be sure, but the most likely cause is that your boundingRect result either isn't correct, or you didn't call prepareGeometryChange before changing the minX, minY, etc. members, or the pen width. If you're accidentally drawing outside of the boundingRect and have the ItemClipsToShape flag on, you could also see behavior like this. Without ItemClipsToShape, your painting would still succeed, but you'd get artifacts as you moved the items. With ItemClipsToShape, the painter just won't draw outside the bounds.