2011-08-03 21 views
0

私はQGraphicsSceneのカスタムQGraphicsItemsをマウスクリックとマウスカーソル座標に追加しようとしています。しかし、アイテムはマウスカーソルと同じ座標に追加されません。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); 
    } 

私はアイテムを挿入していますする座標は自分のアプリケーションのようにシーンをシーンをGraphicsViewに属していないことを推測してい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); 
     } 

ビュー全体をカバーしていません。しかし、私の場合、どのようにシーンの座標を取得できますか?

答えて

1

そうだね、座標はグラフィック・ビューに対して相対的ではなく、シーン

Qt's documentationから撮影:

は受け取ったウィジェットに対する、マウスカーソルの位置を返します。イベント。 マウスイベントの結果としてウィジェットを移動する場合、振るのを避けるためにglobalPos()によって返されるグローバル位置を使用します。

うまくいけば、彼らは便利な機能(excerpt from the QGraphicsView doc)を提供:

ます。またQGraphicsViewのサブクラスを作成し、マウスとキーイベントハンドラを再実装することで、独自のカスタムシーンの相互作用を提供することができます。ビュー内のアイテムとプログラムでどのようにやり取りするかを簡単にするために、QGraphicsViewはmapToScene()およびmapFromScene()、およびアイテムアクセサのitems()およびitemAt()のマッピング関数を提供します。これらの関数を使用すると、ビュー座標とシーン座標の間の点、矩形、ポリゴン、およびパスをマッピングし、ビュー座標を使用してシーン上の項目を見つけることができます。気をつけろ、mapToScene()QPointF、ないQPoint返します

だから、あなたが探している機能はrenderAreaが編集

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

QGraphicsView

から継承するので、あなたが直接呼び出すことができ mapToScene()です。それ自体は問題ではありませんが、これに気づくべきです。

関連する問題