本文介绍了修改自定义 QGraphicsItem 时显示的工件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个小型矢量绘图程序,您可以在其中创建线条并在创建后修改它们(这些线条基于自定义 QGraphicsItem).例如,下图显示了将线的最左侧(标记为黄色)点拖动到屏幕右侧时发生的情况,有效地延长了线:

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 :

我用来调用重绘的代码段位于 mouseMoveEvent 重新定义的方法中,该方法包含以下代码行:

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);

我的 boundingRect 定义显然没有问题,因为在 paint 方法中添加 painter->drawRect(boundingRect()) 显示了这一点:

There's apparently no problem with my boundingRect definition, since adding painter->drawRect(boundingRect()) in the paint method shows this :

而且当线被简单地移动(标记 QGraphicsItem::ItemIsMovable 被设置)时也没有问题,甚至很快.

And there are also no problem when the line is simply moved (flag QGraphicsItem::ItemIsMovable is set), even rapidly.

有人知道这里发生了什么吗?我的猜测是 update 没有被立即调用,因此在重绘发生之前可以多次调用 mouseMoveEvent ,也许取消以前的调用?我不确定.

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.

当然,简单的解决方法是将保持该行的 QGraphicsView 对象的视口模式设置为 QGraphicsView::FullViewportUpdate),但这很丑陋(而且速度很慢).

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).

推荐答案

如果没有看到有关如何更新行的完整功能,我猜您已经省略了调用 prepareGeometryChange() 在更新项目的边界矩形之前.

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.

如文档所述:-

为几何变化准备项目.在更改项目的边界矩形之前调用此函数以保持 QGraphicsScene 的索引最新.

这篇关于修改自定义 QGraphicsItem 时显示的工件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 11:16