Results 1 to 2 of 2

Thread: QGraphicsView drawing

  1. #1
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QGraphicsView drawing

    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

  2. #2
    Join Date
    Jan 2007
    Posts
    81
    Thanks
    17
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsView drawing

    first off, thanks for trying to help with my thread

    1) When I get positions in my code I always do (for instance):
    m_firstPt = mapToScene(e->pos());

    this sets the m_firstPt to the place where the mouse is in terms of scene coordinates....and since you are drawing to m_canvas, I think that should help out a little...

    also make sure that in graphicView class you do the following: m_canvas = new QGraphicsScene(this);

    So to answer your question...this is possibly why the placement on the scene (aka canvas) may look weird? If this doesn't help, please say how the lines get changed...

    2) you want the lines to appear like a rubberband? Then set the pen of m_edgeItem to a dotted line....using: setPen(QPen)

    For what it's worth in my program I don't re-implement drawItems.....they should be drawn automatically when you add them to the scene.

    hth.

    Cheers,

    Jonathan

Similar Threads

  1. Drawing on QGraphicsView
    By JonathanForQT4 in forum Qt Programming
    Replies: 28
    Last Post: 8th June 2007, 15:43
  2. Drawing the background of QGraphicsView
    By aknuds1 in forum Qt Programming
    Replies: 13
    Last Post: 9th March 2007, 14:53
  3. Using QGraphicsView with model/view programming
    By JLP in forum Qt Programming
    Replies: 3
    Last Post: 29th January 2007, 11:04
  4. Drawing on QWidget - strech & resize
    By kemp in forum Qt Programming
    Replies: 5
    Last Post: 22nd January 2007, 14:39
  5. How to use QGraphicsView and Scene right ...
    By Mike in forum Qt Programming
    Replies: 6
    Last Post: 22nd January 2007, 08:51

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.