繁体   English   中英

创建自定义QGraphicsItem时拦截鼠标事件

[英]intercept mouse events when creating a custom QGraphicsItem

我正在使用标准的QGraphicsView和QGraphicsScene,但为了能够在ImagePixmapItem上使用鼠标事件,将QGraphicsPixmapItem子类化为自己的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);

但这没有任何好处。

您可以使用事件过滤器。

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

对于您的原始示例,我还要补充一点,请确保在相关小部件上启用鼠标跟踪。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM