2013-05-04 29 views
1

我有一個QGraphicsScene存儲QGraphicsLinesItems和QGraphicsRectItems。使用QGraphicsScene ItemAt()來檢測QGraphicsLineItem

我使用QGraphicsScene方法itemsAt()和我通過x和y座標ordiantes返回的QGraphicsItem我用的是以下幾點:

 QPointF getItemPos= this->mapToScene(this->mapFromGlobal(QCursor::pos())); 
     QGraphicsItem *itm = scene->itemAt(getItemPos.x(),getItemPos.y()); 
     QGraphicsLineItem *lineItm; 
     QGraphicsRectItem *rectItm; 
     if((lineItm = dynamic_cast<QGraphicsLineItem*>(itm))){ 
      // do stuff with as_pnedge 
      qDebug("Line"); 
     }else if((rectItm = dynamic_cast<QGraphicsRectItem*>(itm))){ 
      // do stuff with as_pnitem 
      qDebug("Rect"); 
     } 
     else 
     { 
      qDebug("Select Item"); 
     } 

我遇到的問題是返回QGraphicsRectItem罰款並且可以被檢測到。但無論我在哪裏點擊QGraphicsLineItems,它都無法檢測並返回該項目。任何援助將非常感激。

回答

1

如果您的線條使用化妝筆(寬度爲零),這意味着shape()方法將返回零寬度QPainterPathsource code,搜索「qt_graphicsItem_shapeFromPath」)。

您將必須從QGraphicsLineItem和實現shape()派生,以將QPainterPathStroker的最小筆寬限制爲合理值。