Results 1 to 5 of 5

Thread: Context Menu & QGraphicsWidget

  1. #1
    Join Date
    Jan 2010
    Location
    Turkey
    Posts
    39
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Context Menu & QGraphicsWidget

    Hi,
    In my application I have two object type. One is field item, other is composite item.
    Composite items may contain two or more field items.
    Here is my composite item implementation.

    Qt Code:
    1. #include "compositeitem.h"
    2.  
    3. CompositeItem::CompositeItem(QString id,QList<FieldItem *> _children)
    4. {
    5. children = _children;
    6. }
    7.  
    8. CompositeItem::~CompositeItem()
    9. {
    10. }
    11.  
    12. QRectF CompositeItem::boundingRect() const
    13. {
    14. //Not carefully thinked about it
    15. return QRectF(QPointF(-50,-150),QSizeF(250,250));
    16. }
    17.  
    18. void CompositeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget )
    19. {
    20. FieldItem *child;
    21. foreach(child,children)
    22. {
    23. child->paint(painter,option,widget);
    24. }
    25. }
    26.  
    27. QSizeF CompositeItem::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
    28. {
    29. QSizeF itsSize(0,0);
    30. FieldItem *child;
    31. foreach(child,children)
    32. {
    33. // if its size empty set first child size to itsSize
    34. if(itsSize.isEmpty())
    35. itsSize = child->sizeHint(Qt::PreferredSize);
    36. else
    37. {
    38. QSizeF childSize = child->sizeHint(Qt::PreferredSize);
    39. if(itsSize.width() < childSize.width())
    40. itsSize.setWidth(childSize.width());
    41. itsSize.setHeight(itsSize.height() + childSize.height());
    42. }
    43. }
    44. return itsSize;
    45. }
    46.  
    47. void CompositeItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
    48. {
    49. qDebug()<<"Test";
    50. }
    To copy to clipboard, switch view to plain text mode 

    My first question is how I can propagate context menu event to specific child.

    composite1.JPG

    Picture on the above demonstrates one of my possible composite item.

    If you look on the code above you will see that I print "Test" when context menu event occurs.

    When I right click on the line symbol I see that "Test" message is printed.
    But when I right click on the signal symbol "Test" is not printed and I want it to be printed.

    My second question what cause this behaviour.
    How do I overcome this.

  2. #2
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Platforms
    Unix/X11 Windows

    Default Re: Context Menu & QGraphicsWidget

    First of all concerning your composite item's ::boudingRect(), you really should "think about it" My guess is to merge every child item's bounding rect.

    To ensure you computed the right boundingRect, add this code in your CompositeItem:aint() :

    Qt Code:
    1. painter->save() ;
    2. painter->setPen(Qt::yellow) ;
    3. painter->setBrush(Qt::NoBrush) ;
    4. painter->drawRect(boundinRect()) ;
    5. painter->restore() ;
    To copy to clipboard, switch view to plain text mode 

    And try to right-click on your composite item, maybe it will suffice (didn't try so I'm not sure)

  3. #3
    Join Date
    Jan 2010
    Location
    Turkey
    Posts
    39
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Context Menu & QGraphicsWidget

    You are right buddy. I think seriously about boundingRect. (I edited the code)

    Qt Code:
    1. QRectF CompositeItem::boundingRect() const
    2. {
    3. FieldItem *child;
    4. QRectF rect(0,0,0,0);
    5. foreach(child,children)
    6. {
    7. rect = rect.united(child->boundingRect());
    8. }
    9. return rect;
    10. }
    To copy to clipboard, switch view to plain text mode 

    I thinks its is a neat idea to unite child items boundingRects but this time
    item can' t catch events like contextmenuevent, mousepressevent.
    I tried to click everywhere inside boundingRect.
    What may cause this?
    Last edited by onurozcelik; 12th May 2010 at 09:05. Reason: updated contents

  4. #4
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Platforms
    Unix/X11 Windows

    Default Re: Context Menu & QGraphicsWidget

    sorry, I've no idea without seeing involved source code. It might come from an event you accept too early in the scene or not forwarded by a virtual method call, or even items are not selectable, etc.

  5. #5
    Join Date
    Jan 2010
    Location
    Turkey
    Posts
    39
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Context Menu & QGraphicsWidget

    I solved the problem temporarily. I rewrite paint. Now it looks as follows.

    Qt Code:
    1. void CompositeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget )
    2. {
    3. painter->setPen(Qt::transparent);
    4. painter->drawRect(boundingRect());
    5.  
    6. FieldItem *child;
    7. foreach(child,children)
    8. {
    9. child->paint(painter,option,widget);
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    Now I can catch events occurs inside rectangle
    Last edited by onurozcelik; 12th May 2010 at 12:09. Reason: spelling corrections

Similar Threads

  1. Context Menu not working
    By waynew in forum Qt Programming
    Replies: 1
    Last Post: 10th January 2010, 03:48
  2. Context menu
    By dejvis in forum Newbie
    Replies: 2
    Last Post: 20th September 2009, 22:02
  3. Qwt and context menu
    By giusepped in forum Qwt
    Replies: 1
    Last Post: 9th December 2008, 08:55
  4. Context Menu
    By RY in forum Newbie
    Replies: 1
    Last Post: 10th September 2008, 07:59
  5. QScrollBar context menu
    By ChasW in forum Qt Programming
    Replies: 1
    Last Post: 29th January 2007, 06:45

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.