我有一个设计的提升图。遍历提升图,我得到了矩形和折线的坐标。通过该坐标,我在 QGraphicsView 上绘制矩形和折线。
现在我想用鼠标点击通过场景选择矩形/折线,并且应该能够打印它的名称和其他属性。它的名称和其他属性在 boost graph 节点中。但我不想再次交互提升图。
因此,当我通过提升图坐标绘制矩形/折线时,我应该能够将提升图节点的一些指针存储在矩形/折线上,这样当我单击矩形/折线时,我可以通过该指针访问它的名称和其他属性。
问题是Is this possible ?
我试过但运行时错误。
升压图类之一。
class schVertexProps
{
public:
explicit schVertexProps(std::string moduleName = "",std::string name = "",
SCH_VERTEX_TYPE type = SCH_VERTEX_TYPE::SCH_INST_VERTEX,
long crossRefPtr = 0,int vertexLevel = -1,int xPos = 0.0,
int yPos = 0.0,schSymbol* symb = nullptr)
: _moduleName(moduleName),
_name(name),
_type(type),
_crossRefPtr(crossRefPtr),
_vertexLevel(vertexLevel),
_xPos(xPos),
_yPos(yPos),
_symb(symb){
};
在上述所有参数中,我只想存储 _crossRefPtr (它基本上是一个指针,但存储为long
,使用时会进行类型转换)
rect = new guiSchematicRect();
while(true)
{
// traversing through boost graph
for(auto iter = verts.begin();iter != verts.end();++iter)
{
// getting co-ordinates of rectangle
QGraphicsRectItem* rectItem = rect->createRect(co-ordinates of rectangle);
rect->_boostRefPtr = iter->_crossRefPtr; // trying to store _crossRefPtr
}
// code somewhat similar for polyline
}
guiSchematicRect.h
class guiSchematicRect : public QGraphicsRectItem
{
........
QGraphicsRectItem* createRect(QRectF& rect);
long _boostRefPtr;
}
当我通过鼠标单击场景上的矩形时,我会这样做:
if(_scene->selectedItems().count() != 0)
{
foreach(QGraphicsItem* currentItem, _scene->selectedItems())
{
QGraphicsRectItem* rItem = qgraphicsitem_cast<QGraphicsRectItem*>(currentItem);
if(rItem)
{
guiSchematicRect* r = reinterpret_cast<guiSchematicRect*>(rItem);
if(r)
{
// I am assuming _boostRefPtr has address of Instance (rectangle)
Instance* i = reinterpret_cast<Instance*>(r->_boostRefPtr);
qDebug()<< i->Name();
}
}
但是aobve方法是错误的。出现运行时错误(显示详细堆栈跟踪)
所以问题是:
如何在 QGraphicsItem 上存储指针,以便一旦它们被选中,该指针就会被访问?
使用 Qt 的动态属性,检查QObject::setProperty
. 它应该可以解决问题。
但是 AFAIC,我会使用双精度QMap
直接关联<graph_node, QGraphicsItem>
AND <QGraphicsItem, graph_node>
- 因此您可以快速搜索这两个关联,并且都具有 O(log2(n)) 复杂度。您可以将其存储为图形的静态部分(更好)或独立(不是最好的主意)。显然,如果您的图表已经在 O(log2(n)) 中,那么您不需要这张地图,而只需要<QGraphicsItem, graph_node>
一张。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句