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???
{
Q_OBJECT
public:
~graphicView(){}
private:
QVector<QLineF> m_vectorLine;
protected:
int numItems,
};
class graphicView : public QGraphicsView
{
Q_OBJECT
public:
graphicView(QGraphicsScene* s);
~graphicView(){}
private:
QPointF m_firstPt;
QPointF m_lastPt;
QGraphicsLineItem* m_edgeItem;
QGraphicsScene* m_canvas;
QVector<QLineF> m_vectorLine;
protected:
void mousePressEvent( QMouseEvent* e );
void mouseMoveEvent( QMouseEvent* e );
void mouseReleaseEvent( QMouseEvent* e );
void drawItems(QPainter *painter,
int numItems,
QGraphicsItem *items[],
const QStyleOptionGraphicsItem options[]);
};
To copy to clipboard, switch view to plain text mode
}
m_firstPt = e->pos();
m_lastPt = e->pos();
}
m_lastPt = e->pos();
}
m_lastPt = e->pos();
m_edgeItem
->setLine
( QLineF( m_firstPt, m_lastPt
) );
m_vectorLine <<
QLineF( m_firstPt, m_lastPt
);
}
void graphicView
::drawItems(QPainter *painter,
int numItems,
painter->setPen(Qt::black );
painter->drawLines( m_vectorLine );
}
graphicView::graphicView(QGraphicsScene* s)
:QGraphicsView(s), m_canvas(s){
m_edgeItem = new QGraphicsLineItem( 0, m_canvas );
}
void graphicView::mousePressEvent( QMouseEvent* e ){
m_firstPt = e->pos();
m_lastPt = e->pos();
}
void graphicView::mouseMoveEvent( QMouseEvent* e ){
m_lastPt = e->pos();
}
void graphicView::mouseReleaseEvent( QMouseEvent* e ){
m_lastPt = e->pos();
m_edgeItem->setLine( QLineF( m_firstPt, m_lastPt ) );
m_vectorLine << QLineF( m_firstPt, m_lastPt );
}
void graphicView::drawItems(QPainter *painter,
int numItems,
QGraphicsItem *items[],
const QStyleOptionGraphicsItem options[]){
painter->setPen(Qt::black );
painter->drawLines( m_vectorLine );
}
To copy to clipboard, switch view to plain text mode
Thanks
Bookmarks