【问题标题】:intercept mouse events when creating a custom QGraphicsItem创建自定义 QGraphicsItem 时拦截鼠标事件
【发布时间】:2011-01-21 18:54:52
【问题描述】:

我使用的是标准的 QGraphicsView 和 QGraphicsScene,但我已将 QGraphicsPixmapItem 子类化为我自己的 ImagePixmapItem,以便能够在 ImagePixmapItem 上使用鼠标事件。

我需要做什么才能捕获这些事件?我已经覆盖了几个这样的函数:

   void ImagePixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event){

       qDebug("hello");

   }

void ImagePixmapItem::wheelEvent ( QGraphicsSceneWheelEvent * event ){
    qDebug("Print this line if catch a wheelEvent");//this is never printing

}

但是,这些 qDebug 语句都不会打印到控制台。我是否需要更改有关我的场景或图形视图的任何其他内容?在 ImagePixmapItem 的构造函数中,我确实添加了一些东西:

setAcceptHoverEvents(true);
setFlag(QGraphicsItem::ItemIsSelectable,true);

但它没有做任何好事。

【问题讨论】:

  • 您能告诉我们您是如何将这些项目添加到 {{QGraphicsScene}} 的吗?

标签: c++ qt qgraphicsview


【解决方案1】:

您可以使用事件过滤器。

http://cartan.cas.suffolk.edu/qtdocs/eventsandfilters.html

我还要为您的原始示例添加这一点,请务必在相关小部件上启用鼠标跟踪。

【讨论】:

    猜你喜欢
    • 2011-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-04
    • 2020-09-01
    • 2022-01-13
    • 1970-01-01
    • 2011-08-28
    相关资源
    最近更新 更多