PDA

View Full Version : Highligt QGraphicsItem on mouse enterevent.



matteo.boscolo
12th June 2010, 10:43
Hi all,
I had implemented the following object :

class BaseEntity(QtGui.QGraphicsItem):

and overwrite the following method:

def hoverEnterEvent(self, event):
self.setHiglight() #this is my function to define the color
self.update()

def hoverLeaveEvent(self, event):
self.setColor() #this is my function to define the color
self.update()

this way dose not work very well if I have some boundingRect() that intersect.

For exsample in case of two circle if I define a circle of radius 10 in position 0,0
and then a circle radius 20 in position 0,0,
the first circle is not higlited when I pass over it with the mouse ..

I try to set the QtGui.QGraphicsItem.ItemClipsToShape flag ... but it do not work at all ..


any idea on how to solve the this problem ?

Regards,
Matteo

wysota
12th June 2010, 13:14
You need to enable hover events for your item - QGraphicsItem::setAcceptHoverEvents() and reimplement QGraphicsItem::shape() for your item.

matteo.boscolo
13th June 2010, 12:47
Thanks,

It works very well now as you can see at :
http://www.youtube.com/user/pythoncad#p/a/u/0/VB3D9yh-MFI


But this introduce me a problem releted to the scene mousePresEvent.
this is my function ..on the qtscene:

def mousePressEvent(self, event):
qtItem=self.itemAt(event.scenePos()) #
if qtItem:
print "item : ", qtItem
else:
print "No item selected"

sometimens and I can't understand why, if the entity is highligted this function print the "No item selected"..

I olso add the setSelected(True) at the item douring the hoverEnterEvent...and of course
self.GraphicsItemFlags(QtGui.QGraphicsItem.ItemIsS electable) douring init ..

Any idea?

Regards,
Matteo

wysota
13th June 2010, 13:37
If an item can't be found at position you expect it to be at then most often this is the case of incorrect implementation of boundingRect() or shape().