2

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.

#include "compositeitem.h"

CompositeItem::CompositeItem(QString id,QList<FieldItem *> _children)
{
   children = _children;
}

CompositeItem::~CompositeItem()
{
}

QRectF CompositeItem::boundingRect() const
{
 FieldItem *child;
     QRectF rect(0,0,0,0);
     foreach(child,children)
     {
        rect = rect.united(child->boundingRect());
     }
    return rect;
}

void CompositeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,   QWidget *widget )
  {
   FieldItem *child;
   foreach(child,children)
   {
      child->paint(painter,option,widget);
   }
  }

  QSizeF CompositeItem::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
  {
   QSizeF itsSize(0,0);
   FieldItem *child;
   foreach(child,children)
   {
      // if its size empty set first child size to itsSize
      if(itsSize.isEmpty())
          itsSize = child->sizeHint(Qt::PreferredSize);
      else
      {
          QSizeF childSize = child->sizeHint(Qt::PreferredSize);
              if(itsSize.width() < childSize.width())
                  itsSize.setWidth(childSize.width());
              itsSize.setHeight(itsSize.height() + childSize.height());
      }
  }
  return itsSize;
     }

     void CompositeItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
     {
          qDebug()<<"Test";
     }

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

composite1

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.

Community
  • 1
  • 1
onurozcelik
  • 1,214
  • 3
  • 21
  • 44

2 Answers2

0

Could you try reimplementing your contextMenu with the mouseRelease event instead?

void CompositeItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
        if(event->button() == Qt::RightButton)
        {
            contextMenu->popup(QCursor::pos());
        }
}
Vicken Simonian
  • 411
  • 4
  • 6
  • Due to an advice I edited boundingRect code. I guess it is a neat idea to unite childs boundingRects. But this time item can' t catch events like contextmenuevent and mousepressevent. So my prior problem is to catch events. Do you have an idea what may cause this? – onurozcelik May 12 '10 at 08:26
0

I figured out that there may be two solutions for catching events. First one is reimplementing shape function. In my case it will be implemented like this.

QPainterPath shape() const
{
  QPainterPath path;
  path.addRect(boundingRect());
  return path;
}

Second one is to use QGraphicsItemGroup
It will be good idea to use QGraphicsItemGroup if you diretly adding your items to scene. But in my case I have to subclass QGraphicsItemGroup because of I am using a layout. So temporarily I choose writing my own item.

onurozcelik
  • 1,214
  • 3
  • 21
  • 44