Results 1 to 8 of 8

Thread: QGraphicsPathItem Doubt

  1. #1
    Join Date
    Jul 2007
    Posts
    39
    Thanks
    10

    Default QGraphicsPathItem Doubt

    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

  2. #2
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsPathItem Doubt

    Reimplement the contains function by deriving a class from QGraphicsPathItem like this

    Qt Code:
    1. class MyPathItem : public QGraphicsPathItem
    2. {
    3. public:
    4. MyPathItem(const QPainterPath & path, QGraphicsItem * parent = 0 ) : QGraphicsPathItem(path,parent)
    5. {}
    6.  
    7. bool contains( const QPointF & point ) const
    8. {
    9. return boundingRect().contains(point);
    10. }
    11. };
    To copy to clipboard, switch view to plain text mode 
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  3. The following user says thank you to Gopala Krishna for this useful post:

    arjunasd (27th July 2007)

  4. #3
    Join Date
    Jul 2007
    Posts
    39
    Thanks
    10

    Default Re: QGraphicsPathItem Doubt

    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

  5. #4
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsPathItem Doubt

    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

    Qt Code:
    1. MyPathItem::paint(QPainter *p, QStyleOptionGraphicsItem *o, QWidget *w)
    2. {
    3. QGraphicsPathItem::paint(p,o,w);
    4. if(o->State & QStyle::State_Selected)
    5. p->setPen(selectedPen);
    6. else
    7. p->setPen(unselectedPen);
    8. p->drawRect(boundingRect());
    9. }
    To copy to clipboard, switch view to plain text mode 
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  6. #5
    Join Date
    Jul 2007
    Posts
    39
    Thanks
    10

    Default Re: QGraphicsPathItem Doubt

    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

  7. #6
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsPathItem Doubt

    In this case , the solution above involving paint event is what you need
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  8. The following user says thank you to Gopala Krishna for this useful post:

    arjunasd (28th July 2007)

  9. #7
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsPathItem Doubt

    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

    Qt Code:
    1. MyPathItem::paint(QPainter *p, QStyleOptionGraphicsItem *o, QWidget *w)
    2. {
    3. QStyle::State back = o->state;
    4. o->state &= ~QStyle::State_Selected;//Removes selection flag
    5. QGraphicsPathItem::paint(p,o,w);
    6. o->state = back;
    7. if(o->state & QStyle::State_Selected)
    8. p->setPen(selectedPen);
    9. else
    10. p->setPen(unselectedPen);
    11. p->drawRect(boundingRect());
    12. }
    To copy to clipboard, switch view to plain text mode 
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  10. #8
    Join Date
    Jul 2007
    Posts
    39
    Thanks
    10

    Default Re: QGraphicsPathItem Doubt

    This is what I wanted. About how to avoid painting the bouding rect twice. Thanks man.

    Arjun

Similar Threads

  1. Doubt ?
    By Cutey in forum Qt Tools
    Replies: 2
    Last Post: 3rd March 2007, 09:45
  2. doubt with this qt's code
    By Gopala Krishna in forum Qt Programming
    Replies: 1
    Last Post: 24th February 2007, 13:39
  3. Basic C++ doubt
    By munna in forum General Programming
    Replies: 8
    Last Post: 30th November 2006, 20:54
  4. Model - View Programming doubt.
    By munna in forum Qt Programming
    Replies: 4
    Last Post: 28th April 2006, 13:01

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.