PDA

View Full Version : how to find nearest QGraphicsitem



dhalbert
29th September 2010, 16:08
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

^NyAw^
29th September 2010, 16:32
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.

dhalbert
30th September 2010, 21:33
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.