QGraphicsWidget 上的上下文菜单

发布于 2024-08-31 21:50:52 字数 1799 浏览 3 评论 0原文

在我的应用程序中,我有两种对象类型。一种是字段项,另一种是复合项。 复合项目可以包含两个或多个字段项目。 这是我的复合项目实现。

#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";
     }

我的第一个问题是如何将上下文菜单事件传播给特定的孩子。

composite1

上面的图片展示了我可能的复合项目之一。

如果您查看上面的代码,您将看到当上下文菜单事件发生时我打印“Test”。

当我右键单击线条符号时,我看到打印了“测试”消息。 但是,当我右键单击信号符号“测试”时,未打印它,而我希望打印它。

我的第二个问题是什么导致了这种行为。 我该如何克服这个问题。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

难以启齿的温柔 2024-09-07 21:50:53

您可以尝试使用 mouseRelease 事件重新实现 contextMenu 吗?

void CompositeItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
        if(event->button() == Qt::RightButton)
        {
            contextMenu->popup(QCursor::pos());
        }
}

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());
        }
}
一口甜 2024-09-07 21:50:53

我发现捕获事件可能有两种解决方案。
第一个是重新实现 shape 函数。
就我而言,它将像这样实施。

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

第二种是使用 QGraphicsItemGroup

如果您直接将项目添加到场景中,那么使用 QGraphicsItemGroup 是个好主意。但就我而言,我必须子类 QGraphicsItemGroup 因为我正在使用布局。
所以我暂时选择自己写一篇。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文