[英]Access other item contents within a QGraphicsScene
I have a dialog.cpp file, it is within here I created my QGraphicsScene
which uses the QGraphicsView
. 我有一个dialog.cpp文件,它是内这里我创建了
QGraphicsScene
它使用QGraphicsView
。
I created two classes that inherit QGraphicsPixmapItem
, "Vehicle"
and "Junction"
. 我创建了两个继承
QGraphicsPixmapItem
类"Vehicle"
和"Junction"
。 Both of these objects are added to the same QGraphicsScene
in my dialog.cpp file. 这两个对象都添加到我的dialog.cpp文件中的同一
QGraphicsScene
中。
I want to call a method which is a member of "Junction"
from my "Vehicle"
object. 我想从我的
"Vehicle"
对象中调用作为"Junction"
成员的方法。 The method returns a boolean value which I need. 该方法返回我需要的布尔值。
This is more of a c++ orientated question, as I'm sure there's an easy solution which makes use of accessing a parent. 这更多的是面向c ++的问题,因为我敢肯定有一个简单的解决方案可以利用访问父级的方法。
Background info on project: Each "Vehicle"
moves along a road and approaches a "Junction"
. 项目的背景信息:每个
"Vehicle"
都沿着道路行驶并接近"Junction"
。 The vehicle must then ask the junction "does my lane have a green light to go?", the junction will then reply "true/false"
. 然后,车辆必须询问路口“我的车道是否有绿灯通行?”,然后路口将回答
"true/false"
。
After reviewing possible solutions via the QT documentation I have partly resolved the issue. 通过QT文档审查了可能的解决方案后,我已部分解决了该问题。
Calling this->scene()
allows access to member functions of the scene, on which your object was added. 调用
this->scene()
可以访问添加了对象的场景的成员函数。 Since I know the co-ordinates of my object I call, this->scene()->itemAt(240,0);
因为我知道我调用的对象的坐标,所以
this->scene()->itemAt(240,0);
to retrieve a pointer to the item. 检索指向该项目的指针。
The pointer returned is the uppermost item on the scene at the co-ordinates supplied. 返回的指针是在提供的坐标处场景中的最高项目。 If this provides a problem alternately use
scene()->items()
to retrieve a list of all the items on the scene. 如果这提供了问题,请交替使用
scene()->items()
检索场景中所有项目的列表。
However, this only gives access to the item and not the vector in which the item is stored. 但是,这仅允许访问该项目,而不能访问存储该项目的向量。 I have been looking into using the
->parent()
function and think this may be the solution, combined with dynamically casting it to the required pointer. 我一直在研究使用
->parent()
函数,并认为这可能是解决方案,并将其动态转换为所需的指针。 Will update the answer if I find a full solution. 如果找到完整的解决方案,将更新答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.