View Full Version : QGraphicsPathItem Doubt
arjunasd
27th July 2007, 16:13
Hi all
I am having a QGraphicsPathItem which is selectable. I am able to select the item by clicking the area defined by its path. But I would like it to be selectable by clicking any point within its bounding rectangle.
For example, if my shape is circle, I want it to be selected if I click on any point its bounding rectangle.
Any suggestion for how to do this?
Thanks
Arjun
Gopala Krishna
27th July 2007, 17:13
Reimplement the contains function by deriving a class from QGraphicsPathItem like this
class MyPathItem : public QGraphicsPathItem
{
public:
MyPathItem(const QPainterPath & path, QGraphicsItem * parent = 0 ) : QGraphicsPathItem(path,parent)
{}
bool contains( const QPointF & point ) const
{
return boundingRect().contains(point);
}
};
arjunasd
28th July 2007, 05:55
One more doubt.
I have the QGraphicsPathItem and its bounding rectangle. I want to change the Pen that draws the bounding rectangle when the item is selected. I dont have the paint function. I just use the default implementation as of now.
How to set the pen for drawing the bounding rectangle when the item is selected. Is there a way to use the default implementation and still achieve this?
Thanks
Arjun
Gopala Krishna
28th July 2007, 07:20
Oh you mean the bounding rect is part of the painterpath ? If thats the case you can't achieve what you wanted. You can draw the bounding rect separately. Then you can control its pen like this
MyPathItem::paint(QPainter *p, QStyleOptionGraphicsItem *o, QWidget *w)
{
QGraphicsPathItem::paint(p,o,w);
if(o->State & QStyle::State_Selected)
p->setPen(selectedPen);
else
p->setPen(unselectedPen);
p->drawRect(boundingRect());
}
arjunasd
28th July 2007, 15:56
Oh you mean the bounding rect is part of the painterpath ?
No. The bouding rectangle is not part of the painterpath. Its just the QRectF returned by the boudingRect fucntion.
The default paint implemnetation draws it with black pen and dotted lines. I want to chnage this behaviour. So, can I just draw the bounding rect with the new pen over the old one? or can I do it differently?
Thanks a lot
Arjun
Gopala Krishna
28th July 2007, 17:28
In this case , the solution above involving paint event is what you need :)
Gopala Krishna
29th July 2007, 03:18
BTW i had a look at the source code of QPainterPathItem and actually that draws the bounding rect. So the above solution will be drawing the bounding rect twice :)
You can use this hack instead
MyPathItem::paint(QPainter *p, QStyleOptionGraphicsItem *o, QWidget *w)
{
QStyle::State back = o->state;
o->state &= ~QStyle::State_Selected;//Removes selection flag
QGraphicsPathItem::paint(p,o,w);
o->state = back;
if(o->state & QStyle::State_Selected)
p->setPen(selectedPen);
else
p->setPen(unselectedPen);
p->drawRect(boundingRect());
}
arjunasd
29th July 2007, 03:30
This is what I wanted. About how to avoid painting the bouding rect twice. Thanks man.
Arjun
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.