如何在 QGraphicsItem 中插入指针,以便在获得选定指针时访问它们?

图沙尔

我有一个设计的提升图。遍历提升图,我得到了矩形和折线的坐标。通过该坐标,我在 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在QGraphicsItem中包装文字?

如何在pyqt5中复制粘贴Qgraphicsitem?

如何在Pyqt5中删除QGraphicsItem

如何从场景子类访问QGraphicsItem的信号

滚动QGraphicsView时如何移动QGraphicsItem?

如何正确删除QGraphicsItem?

Qt-如何获得QGraphicsItem相对于场景的位置

如何使用 QGraphicsItem 仅在圆形中绘制网格线

如何对QGraphicsItem使用局部坐标

如何更改QGraphicsItem对象的颜色?

如何禁用qgraphicsitem的多项选择?

防止QGraphicsItem中的字体缩放

如何在Qt中使用来自QGraphicsItem的itemChange

转换后如何访问我的自定义 QGraphicsItem 的数据属性的内容

当我已将指针属性全部初始化时,如何将QGraphicsItem转换为已创建的类?

如何将在 QGraphicsItem 中绘制的像素图添加到 QGraphicsScene?

如何获取QGraphicsItem中带有孔的形状的鼠标悬停事件?

如何获取QGraphicsItem坐标系中的光标单击位置?

如何找到更接近鼠标 x 或鼠标 y 坐标的 QGraphicsItem(在 QList 中)?

QGraphicsItem::paint():如何检查是否打印了 QGraphicsScene

如何使QGraphicsItem具有半透明颜色?

遍历QGraphicsItem项时生成错误

在缩放 QGraphicsItem 时设置转换点

QGraphicsItem :: pos在布局中返回0

是否可以在paint()方法中绘制QGraphicsItem?

在Qt QGraphicsItem中实现OpenGL推荐

在QGraphicsScene中连续移动QGraphicsItem并检查Collision

如何使QGraphicsItem的位置依赖于另一个QGraphicsItem?

如何通过在两个QGraphicsItem之间画线来连接两个QGraphicsItem(使用鼠标)