PDA

View Full Version : How to enlarge the grap mouse selection



matteo.boscolo
10th August 2010, 19:58
Hi All,
I have a QGraphicsScene in witch I add some items like line etc etc ..

Each item derived from QGraphicsItem and implements the hoverEnterEvent and hoverLeaveEvent to change the color of the entity.
Whit this system I can make an highlight when the mouse pass over the item.

Sometimes to highlight the entity I need to go very close to the entity and in case of thin line it's very difficult to get the entity highlighted and selected.

so there is any way to enlarge the mouse grap ?
I try to use the scene.setSelectionArea function passing a bigger rectangle in this way under the scene.mouseMoveEvent



path=QtGui.QPainterPath()
path.addRect(scenePos.x()-self.__grapWithd/2, scenePos.y()+self.__grapWithd/2, self.__grapWithd, self.__grapWithd)
self.setSelectionArea(path)


but this code dose not give me any help ..

any help ?
Thanks,
Matteo

tobi
10th August 2010, 22:12
Hi,

you might want to overload QGraphicsItem::shape(), which is used by the graphics system to determine if the mouse is actually inside an item:

class SomeItem : public QGraphicsItem
{
...
// here is how to enlarge the "grab" area by 5 pixels in either dimension:
QPainterPath shape() {
QPainterPath path;
path.addRect( boundingRect().adjusted( -5, -5, 5, 5 ) );
return path;
}
};


Tobi

matteo.boscolo
10th August 2010, 22:47
Thanks for you suggestion but I already overwrite the shape method.
And if I do as you suggest the bounding rectangle dose not fit well my shape.
so in case of overlapping bounding box I do not have the correct highlight.

Overloading the shape method is the only way to enlarge the grap mouse ?

Thanks,
Matteo

tobi
10th August 2010, 23:38
I was only pointing out the general principle of how this could be done. Of course if you have an arbitrarily shaped item, creating a QPainterPath containing only an enlarged QRectF will be rather crude. As far as I know, the shape() function is the way to go. You might have to be more specific on how you define the PainterPath, to, for example, better fit a single line. Be creative :).

matteo.boscolo
13th August 2010, 15:10
Thanks for your responce ...

I' like to be creative :) and happy too :) ..

I found an easy solution :-)


this is my base class for item:


class BaseEntity(QtGui.QGraphicsItem):
...
...

def shape(self):
"""
overloading of the shape method
"""
painterStrock=QtGui.QPainterPathStroker()
path=QtGui.QPainterPath()
self.drawShape(path)
painterStrock.setWidth(self.lineWith)
path1=painterStrock.createStroke(path)
return path1

...
..


I derive my object from the above one : and for example the this is the arc one :


from Interface.Entity.base import *

class Arc(BaseEntity):
"""
this class define the arcQT object
"""

.....


def drawShape(self, painterPath):
"""
overloading of the shape method
"""
painterPath.arcTo(self.boundingRect(),self.startAn gle,self.spanAngle)

......



I change the



painterStrock.setWidth(self.lineWith)


in to




painterStrock.setWidth(self.lineWith*10)


This enlarge the with of the shape :-)

Regards,
Matteo