[Qt-interest] Howto Do `hit Test' with QGraphicsItems ?

Sean Harmer sean.harmer at maps-technology.com
Thu May 6 10:18:50 CEST 2010


On Thursday 06 May 2010 02:37:49 Aaron Lewis wrote:
> On 05/05/2010 07:05 PM, Sean Harmer wrote:
> > Hi,
> > 
> > On Wednesday 05 May 2010 11:47:53 Aaron Lewis wrote:
> >> Hi,
> >> 
> >> 	I'm deploying a small game , which i need to set up some `edges'. 
I've
> >> 
> >> learned the term hitTest from Flash , kinda of collision detections.
> >> 
> >> 	e.g I have a wall around a map , and player should not get out of the
> >> 
> >> yard , any move towards a wall is disallowed.
> >> 
> >> 	So far what i tried is to take advantage of coordinate system.
> >> 	Rely on each items' pos() function with their image size , not so 
good
> >> 
> >> anyway.
> >> 
> >> 	Does anyone has any good ideas ?
> >> 	
> >> 	Appreciate any of your help ;-)
> > 
> > What you are looking for is QPainterPath QGraphicsItem::shape() const:
> > 
> > http://doc.qt.nokia.com/4.7-snapshot/qgraphicsitem.html#shape
> 
> Yeah , useful for setting a rectangle bound of an QGraphicsItem.

Not just a rect, you can set any shape. The boundingRect() function must be 
overridden to provide the bounding rect. The shape() function allows you 
greater control over your bounding region for hit detection.

Cheers,

Sean



More information about the Qt-interest-old mailing list