Hi guys,
I have written the given below sample program. I have two questions

1) I draw some lines (straight lines). Why the coordinates of the line gets changed???
2) The line should appear like rubberBand on mouseMoveEvent too . How to achieve it???

Qt Code:
  1. class graphicView : public QGraphicsView
  2. {
  3. Q_OBJECT
  4. public:
  5. graphicView(QGraphicsScene* s);
  6. ~graphicView(){}
  7. private:
  8. QPointF m_firstPt;
  9. QPointF m_lastPt;
  10. QGraphicsLineItem* m_edgeItem;
  11. QGraphicsScene* m_canvas;
  12. QVector<QLineF> m_vectorLine;
  13. protected:
  14. void mousePressEvent( QMouseEvent* e );
  15. void mouseMoveEvent( QMouseEvent* e );
  16. void mouseReleaseEvent( QMouseEvent* e );
  17. void drawItems(QPainter *painter,
  18. int numItems,
  19. QGraphicsItem *items[],
  20. const QStyleOptionGraphicsItem options[]);
  21.  
  22.  
  23. };
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. graphicView::graphicView(QGraphicsScene* s)
  2. :QGraphicsView(s), m_canvas(s){
  3. m_edgeItem = new QGraphicsLineItem( 0, m_canvas );
  4. }
  5.  
  6. void graphicView::mousePressEvent( QMouseEvent* e ){
  7. m_firstPt = e->pos();
  8. m_lastPt = e->pos();
  9. }
  10. void graphicView::mouseMoveEvent( QMouseEvent* e ){
  11. m_lastPt = e->pos();
  12. }
  13. void graphicView::mouseReleaseEvent( QMouseEvent* e ){
  14. m_lastPt = e->pos();
  15. m_edgeItem->setLine( QLineF( m_firstPt, m_lastPt ) );
  16. m_vectorLine << QLineF( m_firstPt, m_lastPt );
  17. }
  18. void graphicView::drawItems(QPainter *painter,
  19. int numItems,
  20. QGraphicsItem *items[],
  21. const QStyleOptionGraphicsItem options[]){
  22. painter->setPen(Qt::black );
  23. painter->drawLines( m_vectorLine );
  24. }
To copy to clipboard, switch view to plain text mode 

Thanks