I'm making a Diagram (Fluxogram) program and for days I'm stuck with this issue:
我正在做一个图表(Fluxogram)程序,有好几天我都在纠结这个问题:
I have a custom QGraphicsScene
that expands horizontally whenever I place an item to it's rightmost area. The problem is that my custom arrows (they inherit QGraphicsPathItem
) disappear from the scene whenever it's boundingRect()
center is scrolled off the view. Everytime the scene expands, both it's sceneRect()
and the view's sceneRect()
are updated as well.
我有一个自定义QGraphicsScene,每当我将一个项目放在它最右边的区域时,它就会水平扩展。问题是,我的自定义箭头(它们继承了QGraphicsPathItem)在boundingRect() center从视图中展开时就会从场景中消失。每次场景扩展时,它的scen直立()和视图的scen直立()也会更新。
I've:
我:
set ui->graphicsView->setViewportUpdateMode(QGraphicsView::FullViewportUpdate)
the item flags QGraphicsItem::ItemIgnoresTransformations
and QGraphicsItem::ItemSendsGeometryChanges
, setActive(true)
on the item as well, and everytime I add an arrow to the scene i call the update(sceneRect())
method. Still, everytime I scroll the view, as soon as the arrow's boundingRect()
center moves away from the view, all the arrow disappears. If I scroll back and the boundingRect() center enters the view, all the arrow appears again.
项目标记QGraphicsItem:: itemignorestransformation和QGraphicsItem::ItemSendsGeometryChanges,在项目上设置active (true),每当我向场景添加箭头时,我就调用update(scen直立()方法。不过,每次我滚动视图时,只要箭头的boundingRect()中心离开视图,所有的箭头就会消失。如果我向后滚动,boundingRect()中心进入视图,所有箭头将再次出现。
Can someone give me a tip of what I might be missing? I've been using Qt's example project diagramscene as reference, so a lot of my code is similar (the "press item toolButton -> click on the scene" relation to insert items, the way they place the arrows to connect the objects,...).
有人能告诉我我可能遗漏了什么吗?我一直在使用Qt的示例项目diagramscene作为参考,所以我的很多代码都是类似的(“按下item toolButton ->单击场景”关系来插入项目,他们放置箭头来连接对象的方式…)。
In the meanwhile I'll try to make a minimal running example that can show what my issue is.
同时,我将尝试创建一个最小的运行示例,以显示我的问题是什么。
2 个解决方案
#1
-1
Your Arrow object inherits from QGraphicsPathItem, which I expect also implements the QGraphicsItem::shape function.
您的箭头对象继承自QGraphicsPathItem,我希望它也实现了QGraphicsItem::shape函数。
Override the shape function in your Arrow class, to return the shape of the item. This, along with the boundingRect is used to collision detection and detection of an item on-screen.
在您的箭头类中覆盖形状函数,以返回项目的形状。这与boundingRect一起用于对屏幕上的项目进行碰撞检测和检测。
In addition, before changing the shape of an item by changing its boundingRect, you need to call prepareGeometryChange.
此外,在通过更改一个项目的boundingRect来更改其形状之前,需要调用prepareGeometryChange。
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.
为几何变化做好准备。在更改项目的边界矩形之前调用此函数,以使QGraphicsScene的索引保持最新。
So, in the Arrow class, store a QRectF called m_boundingRect and in the constructor: -
因此,在Arrow类中,存储一个名为m_boundingRect的QRectF,并在构造函数中:-
prepareGeometryChange();
m_boundingRect = QRectF(-x, -y, x*2, y*2);
Then return m_boundingRect in the boundingRect() function.
然后在boundingRect()函数中返回m_boundingRect。
If this is still an issue, I expect it's something in QGraphicsPainterPath that's causing the problem, in which case, you can simply inherit from QGraphicsItem and store a QPainterPath with which you draw in the item's paint function and also return the painter path in shape().
如果这仍然是一个问题,我希望是QGraphicsPainterPath中的一些东西导致了这个问题,在这种情况下,您可以简单地从QGraphicsItem继承并存储一个QPainterPath,您可以用它来绘制项目的paint函数,并返回shape()中的painter路径。
#2
0
You are making your life too complicated. Do not subclass QGraphicsPathItem
just use it and update its path
value every time position of anchors (from to) changes.
你的生活太复杂了。不要将QGraphicsPathItem子类化,只需使用它,并在每次锚点(从到)更改位置时更新其路径值。
#1
-1
Your Arrow object inherits from QGraphicsPathItem, which I expect also implements the QGraphicsItem::shape function.
您的箭头对象继承自QGraphicsPathItem,我希望它也实现了QGraphicsItem::shape函数。
Override the shape function in your Arrow class, to return the shape of the item. This, along with the boundingRect is used to collision detection and detection of an item on-screen.
在您的箭头类中覆盖形状函数,以返回项目的形状。这与boundingRect一起用于对屏幕上的项目进行碰撞检测和检测。
In addition, before changing the shape of an item by changing its boundingRect, you need to call prepareGeometryChange.
此外,在通过更改一个项目的boundingRect来更改其形状之前,需要调用prepareGeometryChange。
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.
为几何变化做好准备。在更改项目的边界矩形之前调用此函数,以使QGraphicsScene的索引保持最新。
So, in the Arrow class, store a QRectF called m_boundingRect and in the constructor: -
因此,在Arrow类中,存储一个名为m_boundingRect的QRectF,并在构造函数中:-
prepareGeometryChange();
m_boundingRect = QRectF(-x, -y, x*2, y*2);
Then return m_boundingRect in the boundingRect() function.
然后在boundingRect()函数中返回m_boundingRect。
If this is still an issue, I expect it's something in QGraphicsPainterPath that's causing the problem, in which case, you can simply inherit from QGraphicsItem and store a QPainterPath with which you draw in the item's paint function and also return the painter path in shape().
如果这仍然是一个问题,我希望是QGraphicsPainterPath中的一些东西导致了这个问题,在这种情况下,您可以简单地从QGraphicsItem继承并存储一个QPainterPath,您可以用它来绘制项目的paint函数,并返回shape()中的painter路径。
#2
0
You are making your life too complicated. Do not subclass QGraphicsPathItem
just use it and update its path
value every time position of anchors (from to) changes.
你的生活太复杂了。不要将QGraphicsPathItem子类化,只需使用它,并在每次锚点(从到)更改位置时更新其路径值。