Results 1 to 7 of 7

Thread: Selecting a QLine

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2006
    Posts
    32
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Selecting a QLine

    Sorry if this is (another ) stupid question but I am wondering if Qt provides a method for determining if the mouse point is over a Line. I can use QPainterPath::contains() to determine if the mouse is inside a polygon which is nice, but I can't find a way of doing it for a single QLine. Am I missing something obvious or is this something I have to do manually?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Selecting a QLine

    You can use math for that.

    Every line is represented by a function y=ax+b and two ending points. Just count if a given point lies on that line ('a' and 'b' can be computed from the ending points and then you just have to check whether the test point doesn't violate this equation).

  3. #3
    Join Date
    Feb 2006
    Posts
    32
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Selecting a QLine

    Yeah, I am crap at maths though! Can you expand on that a bit?

    It's strange that Qt doesn't provide a method for this as most other graphical elements have a "contains" method for this sort of thing. Is there a specific reason why QLine doesn't have a method?

  4. #4
    Join Date
    Jan 2006
    Posts
    22
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Selecting a QLine

    Qt Code:
    1. bool distancePointLine( const QPoint &p, const QPoint &l1,
    2. const QPoint &l2, float &distance )
    3. {
    4. float lineMag;
    5. float u;
    6. QPoint intersectPnt;
    7.  
    8. lineMag = magnitude( l2, l1 );
    9.  
    10. u = ( ( ( p.x() - l1.x() ) * ( l2.x() - l1.x() ) ) +
    11. ( ( p.y() - l1.y() ) * ( l2.y() - l1.y() ) ) ) /
    12. ( lineMag * lineMag );
    13.  
    14. if ( u < 0.0f || u > 1.0f )
    15. return false; // closest point does not fall within the line segment
    16.  
    17. intersectPnt.setX( int( l1.x() + u * ( l2.x() - l1.x() ) ) );
    18. intersectPnt.setY( int( l1.y() + u * ( l2.y() - l1.y() ) ) );
    19.  
    20. distance = magnitude( p, intersectPnt );
    21.  
    22. return true;
    23. }
    24.  
    25. float magnitude( const QPoint &p1, const QPoint &p2 )
    26. {
    27. float vx, vy;
    28.  
    29. vx = p2.x() - p1.x();
    30. vy = p2.y() - p1.y();
    31.  
    32. return sqrt( vx * vx + vy * vy );
    33. }
    To copy to clipboard, switch view to plain text mode 
    This code is not the nicest, but it works.

    Returns true of the point p is on the line segment defined by l1 and l2. The distance contains the distance between the point and the line.

  5. #5
    Join Date
    Feb 2006
    Posts
    32
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Selecting a QLine

    Sorry, orb9 but I have tried your code and it doesn't work - I have checked it and rechecked it and examined the code to see whats going on but I can't figure it out.

    Basically, the method almost always returns true - sometimes it's false, but I can't determine a pattern based on where I click in the client area!

  6. #6
    Join Date
    Feb 2006
    Posts
    32
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Selecting a QLine

    I have just found QLineF::interesect() which does exactly what I need:

    Qt Code:
    1. bool Line::hitTest(const QPoint& point) const
    2. {
    3. QPointF intersectPnt;
    4. QLineF line(point.x()-10, point.y()-10, point.x()+10, point.y()+10);
    5.  
    6. return (m_line.intersect(line, &intersectPnt)==QLineF::BoundedIntersection);
    7. }
    To copy to clipboard, switch view to plain text mode 

    Thankyou for your help guys - problem solved

  7. #7
    Join Date
    Sep 2015
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Selecting a QLine

    Might be helpful:

    Qt Code:
    1. bool LineObject::pick_line(const QPointF &p) const
    2. {
    3. // get distance between point and current line
    4. qreal d = dist(p, m_line);
    5.  
    6. // taking into account view scale factor,
    7. // assume scale with constant aspect ratio (sx=sy)
    8. // and we have only one view
    9. const QGraphicsView *pview = graphics_view();
    10. if (NULL != pview)
    11. d *= (0.5*(pview->transform().m11() + pview->transform().m22()));
    12.  
    13. // line picked if point is close to it
    14. return (d < std::max(s_selection_point_size, m_pen.widthF()));
    15. }
    16.  
    17. qreal LineObject::dist(const QPointF &p, const QLineF &line) const
    18. {
    19. // transform to loocal coordinates system (0,0) - (lx, ly)
    20. QPointF p1 = line.p1();
    21. QPointF p2 = line.p2();
    22. qreal x = p.x() - p1.x();
    23. qreal y = p.y() - p1.y();
    24. qreal x2 = p2.x() - p1.x();
    25. qreal y2 = p2.y() - p1.y();
    26.  
    27. // if line is a point (nodes are the same) =>
    28. // just return distance between point and one line node
    29. qreal norm = sqrt(x2*x2 + y2*y2);
    30. if (norm <= std::numeric_limits<int>::epsilon())
    31. return sqrt(x*x + y*y);
    32.  
    33. // distance
    34. return fabs(x*y2 - y*x2) / norm;
    35. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 1
    Last Post: 29th November 2008, 12:37
  2. Trouble with cursor and selecting text in QTextEdit
    By R_Torsten in forum Qt Programming
    Replies: 3
    Last Post: 7th June 2008, 19:17
  3. QListWidget and selecting an item
    By invictus in forum Newbie
    Replies: 4
    Last Post: 19th June 2007, 11:59
  4. selecting lots of treewidget itemns slow ...
    By jh in forum Qt Programming
    Replies: 15
    Last Post: 19th June 2006, 18:52
  5. Signal problem in Qline Edit
    By awalesminfo in forum Newbie
    Replies: 1
    Last Post: 3rd April 2006, 09:13

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.