Results 1 to 3 of 3

Thread: Drawing lines using QGraphicsLineItem and setting its position

  1. #1
    Join Date
    Apr 2014
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Android

    Question Drawing lines using QGraphicsLineItem and setting its position

    Hi All,

    I'm trying to follow this tutorial: http://www.informit.com/articles/art...74421&seqNum=4
    Works pretty well until the point when I try to draw a line between my two rectangles. The position from my two nodes seem to be off.
    I've recorded a video to explain the problem a little better ( Sorry for the video quality :/ ) :


    Selecting 480p makes the text somewhat readable. The rectangle is always showing the current position in the scene, which is the same that is used to set the lines position.

    Here's the code for setting the line:
    Qt Code:
    1. void Link::trackNodes()
    2. {
    3. setLine(QLineF(myFromNode->pos(), myToNode->pos()));
    4. }
    To copy to clipboard, switch view to plain text mode 

    And here's how I setup the scene:
    Qt Code:
    1. //Set-up the scene
    2. QGraphicsScene* scene = new QGraphicsScene(this);
    3. //Set-up the view
    4.  
    5. QGraphicsRectItem *scene_rect_item = new QGraphicsRectItem();
    6. QRectF scene_rect(0,0, 300, 300);
    7. scene_rect_item->setRect(scene_rect);
    8. setSceneRect(scene_rect);
    9. scene->addItem(scene_rect_item);
    10.  
    11. setDragMode(QGraphicsView::RubberBandDrag);
    12. setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);
    13. setContextMenuPolicy(Qt::ActionsContextMenu);
    14.  
    15. setScene(scene);
    To copy to clipboard, switch view to plain text mode 

    Any ideas what might cause this?

    Thank you in advance!

  2. #2
    Join Date
    Apr 2014
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Android

    Default Re: Drawing lines using QGraphicsLineItem and setting its position

    If somebody has the same problem, this is how I've solved it:

    Qt Code:
    1. QPointF pf = mapToItem(mFromNode, pos());
    2. QPointF pt = mapToItem(mToNode, pos());
    3. setLine(pf.x(), pf.y(), -pt.x(), -pt.y());
    To copy to clipboard, switch view to plain text mode 

    If you map the items to its parent, it should work. Its not clearly obvious to my why this is, especially that I have to negate the second position.
    I couldn't find an explanation for this behavior in the docs. Seems like the parent / child relationship works in a different way that I'm used to.

  3. #3

    Default Re: Drawing lines using QGraphicsLineItem and setting its position

    Thanks! Your post has saved my day!

    Quote Originally Posted by fusion44 View Post
    If somebody has the same problem, this is how I've solved it:

    Qt Code:
    1. QPointF pf = mapToItem(mFromNode, pos());
    2. QPointF pt = mapToItem(mToNode, pos());
    3. setLine(pf.x(), pf.y(), -pt.x(), -pt.y());
    To copy to clipboard, switch view to plain text mode 

    If you map the items to its parent, it should work. Its not clearly obvious to my why this is, especially that I have to negate the second position.
    I couldn't find an explanation for this behavior in the docs. Seems like the parent / child relationship works in a different way that I'm used to.

Similar Threads

  1. QGraphicsLineItem - setting its points
    By Durkin in forum Newbie
    Replies: 1
    Last Post: 5th March 2014, 02:10
  2. Drawing lines on qwtpolarplot
    By jerrychan in forum Qwt
    Replies: 3
    Last Post: 12th June 2013, 11:55
  3. drawing lines using qpainter from file
    By neutrino in forum Qt Programming
    Replies: 2
    Last Post: 9th February 2013, 10:03
  4. Drawing row/column lines in QTreeWidget
    By warvimo in forum Newbie
    Replies: 0
    Last Post: 25th May 2012, 18:33
  5. Setting the QGraphicsLineItem tickness.....
    By dreamer in forum Qt Programming
    Replies: 1
    Last Post: 9th July 2008, 13:11

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.