Results 1 to 3 of 3

Thread: how to find nearest QGraphicsitem

  1. #1
    Join Date
    Jun 2010
    Posts
    8
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default how to find nearest QGraphicsitem

    QGraphicsView::itemAt(QPoint) or QGraphicsView::items(QPoint) will tell me the item or items at the given point.

    I have items that are dots: tiny QGraphicsRectItems. I would like the user to be able to click near a dot to designate it, rather have to click exactly on top of it. So I would like to find the nearest item, within a given halo around the item.

    I miss Tk's find_closest() function, which did exactly this. I could just iterate through all my items and calculate the distance: there are less than a hundred, usually. Or I could overlay my dots with larger transparent QGraphicsItems that define the selection halo I want.

    Any other suggestions? Am I missing some functionality that already exists in the Qt graphics world?

    Thanks,
    Dan

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to find nearest QGraphicsitem

    Hi,

    Create your own QGraphicsItem class and redefine "boundingRectangle". I think that there is an old exemple that uses QCanvas(Qt 3) that does this.
    Òscar Llarch i Galán

  3. The following user says thank you to ^NyAw^ for this useful post:

    dhalbert (29th September 2010)

  4. #3
    Join Date
    Jun 2010
    Posts
    8
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to find nearest QGraphicsitem

    I ended up using ^NyAw^'s method, which worked well for me. I noticed later that an alternative I should have seen: there are versions of QGraphicsView::items() and QGraphicsScene::items() that take a rectangle instead of a single point and return items that are in that rectangle. So those functions could be used to provide a "fat" mouse cursor that would serve the same function.

Similar Threads

  1. How to find the nearest point on a QPainterPath
    By Markus in forum Qt Programming
    Replies: 1
    Last Post: 8th June 2010, 23:18
  2. QTreeView: Find nearest item to mouse pointer
    By squidge in forum Qt Programming
    Replies: 3
    Last Post: 31st October 2009, 20:24
  3. Casting QGraphicsItem child from QGraphicsItem
    By patrik08 in forum Qt Programming
    Replies: 3
    Last Post: 29th August 2008, 15:37
  4. Replies: 2
    Last Post: 28th June 2008, 16:31
  5. Help this guy find its way.
    By Dg!Mortal in forum Newbie
    Replies: 3
    Last Post: 12th April 2008, 18:31

Tags for this Thread

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.