在 qgraphicsview 中忽略 svg 图像透明部分上的鼠标事件?

发布于 2024-10-17 19:30:41 字数 164 浏览 7 评论 0原文

我正在开发一个图形视图(使用 C++ 和 Qt),其中包含相当多的 svg 图像。我拦截对它们的点击,但当鼠标悬停在 svg 项目的透明部分上时,我不想接收事件(或能够忽略它们)。

有可能吗?
svg 文件是否应该专门设计用于此类用途?
是否有一些我还没有听说过的隐藏 Qt 选项?

I am working on a graphics view (using C++ and Qt) which contains quite a few svg images. I intercept clicks on them, but i'd like not to receive events (or to be able to ignore them) when mouse is over transparent parts of svg items.

Is it possible ?
Should svg files be specifically designed for such use ?
Is there some hidden Qt option i have not (yet) heard of ?

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

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

发布评论

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

评论(3

热鲨 2024-10-24 19:30:41

有一个 CSS 属性可以应用于 SVG 元素,pointer-events< /code>,尽管默认值为 visiblePainted

当“visibility”属性设置为可见并且指针位于“绘制”区域上方时,给定元素可以是指针事件的目标元素。如果指针位于元素的内部(即填充)上方并且“填充”属性具有除“无”之外的实际值,或者指针位于元素的周边(即描边)上方并且“笔划”属性设置为除“无”以外的值。

这表明 Qt 图形视图不支持它。

There's a CSS property which can be applied to SVG elements, pointer-events, though the default for this is visiblePainted:

The given element can be the target element for pointer events when the ‘visibility’ property is set to visible and when the pointer is over a "painted" area. The pointer is over a painted area if it is over the interior (i.e., fill) of the element and the ‘fill’ property has an actual value other than none or it is over the perimeter (i.e., stroke) of the element and the ‘stroke’ property is set to a value other than none.

Which would indicate that Qt graphics view doesn't support it.

空名 2024-10-24 19:30:41

没有其他选择,只能以艰难的方式找到我的问题的答案,这就是我所做的:

  • 在 QGraphicsSvgItem.cpp 中查找 mousePressEvent 定义。没有找到。
  • 在 QGraphicsItem.cpp (QGraphicsSvgItem 的祖先)中查找 mousePressEvent 定义。该方法存在,但找不到相关操作。
  • 在 QGraphicsItem.cpp 中查找 mousePressEvent 调用。我发现自己正在阅读 QGraphicsItem::sceneEvent() 的代码,它是 Qt 图形场景的鼠标事件调度程序。图形项目的不同区域似乎没有任何区别。

因此,令人悲伤的答案是:Qt 不允许这种行为。

Having no other choice but to find out the hard way the answer to my question, here is what i did :

  • looked for mousePressEvent definition in QGraphicsSvgItem.cpp. Found none.
  • looked for mousePressEvent definition in QGraphicsItem.cpp (ancestor of QGraphicsSvgItem). The method exists but no relevant action could be found there.
  • looked for mousePressEvent calls in QGraphicsItem.cpp. Found myself reading the code of QGraphicsItem::sceneEvent(), dispatcher of mouse events for a Qt graphics scene. There does not seem to be any sort of differentiating different zones of graphic items.

Hence the sad answer : Qt does not permit such a behaviour.

乖乖 2024-10-24 19:30:41

要完成其他答案:

重新实现事件时,在默认情况下调用基类事件非常重要,否则,未绘制部分的事件透明度就会丢失。

例如

virtual void mouseReleaseEvent( QGraphicsSceneMouseEvent *e) override
{
    if (/* any condition*/)
    {
        // Do some specific behaviour
    }
    else QGraphicsItem::mouseReleaseEvent(e);
}

To complete other answers:

When re-implementing events, it is important to call the base-class event for default cases, if not, the event transparency over non-painted parts is lost.

E.g.

virtual void mouseReleaseEvent( QGraphicsSceneMouseEvent *e) override
{
    if (/* any condition*/)
    {
        // Do some specific behaviour
    }
    else QGraphicsItem::mouseReleaseEvent(e);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文