2011-08-03 49 views
0

我想添加一些自定義QGraphicsItems在QGraphicsScene鼠標點擊和鼠標光標座標。但是這些項目不會以與鼠標光標相同的座標添加。如何在QGraphicsScene中添加項目?

 

    renderArea::renderArea(QWidget *parent): 
      QGraphicsView(parent) 
    { 
     scene = new QGraphicsScene(this); 
     scene->setItemIndexMethod(QGraphicsScene::NoIndex); 
     scene->setSceneRect(0, 0, 850, 480); 
     setScene(scene); 
     setCacheMode(CacheBackground); 
     setViewportUpdateMode(BoundingRectViewportUpdate); 
     setRenderHint(QPainter::Antialiasing); 
     setTransformationAnchor(AnchorUnderMouse); 
     scale(qreal(1.0), qreal(1.0)); 
     setMinimumSize(400, 400); 
    } 

    void renderArea::mousePressEvent(QMouseEvent *event) 
    { 
     QPoint p = event->pos(); 

    updateList(p); 
    } 

    void renderArea::updateList(const QPoint &p) 
    { 
     Point point; 
     point.point = p; 
     point.isSelected = false; 
     list.append(point); 
     if (list.size() > 1) 
      updateClothoid(list[list.size()-2].point, list[list.size()-1].point); 
    } 

    void renderArea::updateClothoid(const QPoint &p1, const QPoint &p2) 
    { 
     Clothoid *temp = new Clothoid(p1, p2); 

     clothoids.append(temp); 

     scene->addItem(temp); 
    } 

renderArea作爲的QGraphicsView和迴旋線定製的QGraphicsItem

 

    Clothoid::Clothoid(QPoint startPoint, QPoint endPoint) 
    { 
     sPoint = startPoint; 
     ePoint = endPoint; 
     startCurvature = 0.0; 
     endCurvature = 0.0; 
     clothoidLength = sqrt(pow(endPoint.x() - startPoint.x(),2) + 
           pow(endPoint.y() - startPoint.y(),2)); 
    }  

    QRectF Clothoid::boundingRect() const 
     { 
      qreal penWidth = 1; 

      if ((sPoint.x() < ePoint.x()) && (sPoint.y() < ePoint.y())) 
       return QRectF(sPoint.x(), sPoint.y(), ePoint.x() - sPoint.x(), ePoint.y()-sPoint.y()) 
       .normalized() 
       .adjusted(-penWidth, -penWidth, penWidth, penWidth); 

      if ((sPoint.x() < ePoint.x()) && (sPoint.y() > ePoint.y())) 
       return QRectF(sPoint.x(), ePoint.y(), ePoint.x() - sPoint.x(), sPoint.y() - ePoint.y()) 
       .normalized() 
       .adjusted(-penWidth, -penWidth, penWidth, penWidth); 

      if ((sPoint.x() > ePoint.x()) && (sPoint.y() < ePoint.y())) 
       return QRectF(ePoint.x(), sPoint.y(), sPoint.x() - ePoint.x(), ePoint.y()-sPoint.y()) 
       .normalized() 
       .adjusted(-penWidth, -penWidth, penWidth, penWidth); 

      if ((sPoint.x() > ePoint.x()) && (sPoint.y() > ePoint.y())) 
       return QRectF(ePoint.x(), ePoint.y(), sPoint.x() - ePoint.x(), sPoint.y() - ePoint.y()) 
       .normalized() 
       .adjusted(-penWidth, -penWidth, penWidth, penWidth); 

      return QRectF(); 

     } 

     void Clothoid::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) 
     { 
      QLineF line(sPoint, ePoint); 

      // Draw the line itself 
      painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); 
      painter->drawLine(line); 
     } 

我猜測座標到我插入的項目屬於GraphicsView,而不是場景在我的應用場景並未涵蓋整個視圖。但是,我怎麼能得到我的情況下的場景座標?

回答

1

你是對的,座標是相對於GraphicView,而不是場景

取自Qt's documentation

返回鼠標光標相對於接收到的插件的位置,事件。 如果您因鼠標事件而移動小部件,請使用globalPos()返回的全局位置以避免發生抖動。

希望他們提供方便的功能(excerpt from the QGraphicsView doc):

您也可以提供自己的自定義場景的互動,通過創建的QGraphicsView的一個子類,並重新實現鼠標和按鍵事件處理函數。爲了簡化以編程方式與視圖中的項目交互的方式,QGraphicsView提供了映射函數mapToScene()和mapFromScene(),以及項目訪問器項目()和itemAt()。這些功能允許您在視圖座標和場景座標之間映射點,矩形,多邊形和路徑,並使用視圖座標查找場景中的項目。

所以,你要尋找的功能是mapToScene(),你可以直接打電話,因爲renderArea從的QGraphicsView

繼承
void renderArea::mousePressEvent(QMouseEvent *event) 
{ 
    QPoint p = mapToScene(event->pos()); 
    updateList(p); 
} 

編輯:當心,mapToScene()返回QPointF,而不是一個QPoint。本身並不是一個問題,但你應該意識到這一點。