Artifacts showing when modifying a custom QGraphicsItem

孤人 提交于 2020-05-29 08:23:21

问题


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 :

Figure1

Everything works fine when the point is moved slowly, however, when moved rapidly, some visual artifacts appear :

enter image description here

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 :

enter image description here

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!