问题
I'm currently developping a small vector drawing program in wich you can create lines and modify them after creation (those lines are based on a custom QGraphicsItem). For instance, the picture below shows what happens when the leftmost (marked yellow) point of the line is dragged to the right of the screen, effectively lengthening the line :
Everything works fine when the point is moved slowly, however, when moved rapidly, some visual artifacts appear :
The piece of code I'm using to call for a repaint is located in the mouseMoveEvent
redefined method, which holds the following lines of code :
QRectF br = boundingRect();
x2 = static_cast<int>(event->scenePos().x()-x());
y2 = static_cast<int>(event->scenePos().y()-y());
update(br);
There's apparently no problem with my boundingRect
definition, since adding painter->drawRect(boundingRect())
in the paint
method shows this :
And there are also no problem when the line is simply moved (flag QGraphicsItem::ItemIsMovable
is set), even rapidly.
Does anyone know what is happening here ? My guess is that update
is not being called immediately hence mouseMoveEvent
can be called multiple times before a repaint occurs, maybe canceling previous calls ? I'm not sure.
Of course the easy fix is to set the viewport mode of the QGraphicsView
object holding the line to QGraphicsView::FullViewportUpdate)
, but that is ugly (and slow).
回答1:
Without seeing the full function for how you're updating the line, I would guess that you've omitted to call prepareGeometryChange() before updating the bounding rect of the items.
As the docs state: -
Prepares the item for a geometry change. Call this function before changing the bounding rect of an item to keep QGraphicsScene's index up to date.
来源:https://stackoverflow.com/questions/26619841/artifacts-showing-when-modifying-a-custom-qgraphicsitem