Results 1 to 4 of 4

Thread: QGraphicItem mouse click detection

  1. #1
    Join Date
    Jan 2006
    Posts
    17
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QGraphicItem mouse click detection

    I have an application that allows the user to draw arrows between boxes using the QGraphics items, views, etc. The arrows are a line with an arrow head drawn on the right hand side, with text at the base of the arrow (non arrow side).

    The problem is that the bounding rectangles overlap a lot when the arrows intersect or become near each other. What is the best way to determine what arrow is closest to the mouse click point? My current method is "ok" in that it works most of the time, but it can be confused and selects the wrong arrow. The method I use is to create a line (not on the screen) from the arrow head to the click point and from the arrow tail to the click point, combine those line lengths and do the same for each arrow found under the click point. The arrow with the least combined length is usually the closest, so I change its color (select it).

    In a nutshell, if a bunch of bounding rectangles intersect, what is the best way to determine which arrow is actually closest to the clicked point on the screen (the bounding rectangles are larger than the line so that they can fit the arrow head and text)?

  2. #2
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicItem mouse click detection

    I have an approach here using QLineF::intersect() . I guess it might work.

    Whenever a user clicks, create a small line with the click position and some neighbouring point. Now iterate through all possible items in clicked area, and check for intersection with the arrow line and above created small line. Since the line is small you may find the nearest arrow.
    Just make sure this checking is done in scene coordintaes.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicItem mouse click detection

    Override the QGraphicsItem::shape() function . This function is used in collision detection, and u can precisely give the shape of arrow in it. Use QPainterPath to define the shape of the arrow.
    I guess this helps

  4. #4
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicItem mouse click detection

    Obviously the above solution from aamer is better than from mine.
    Somehow this didn't strike to my mind
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

Similar Threads

  1. Disable QTextCursor Mouse click repositioning
    By VireX in forum Qt Programming
    Replies: 2
    Last Post: 3rd April 2007, 08:08
  2. The event fired by the mouse click on the frame
    By Placido Currò in forum Qt Programming
    Replies: 8
    Last Post: 3rd March 2007, 09:05
  3. Replies: 1
    Last Post: 9th February 2007, 09:41
  4. mouse click in QGprahicsScene
    By Morea in forum Qt Programming
    Replies: 11
    Last Post: 21st December 2006, 08:21
  5. mouse click event
    By vijay anandh in forum Qt Programming
    Replies: 1
    Last Post: 1st May 2006, 09:24

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.