开发者

intercept mouse events when creating a custom QGraphicsItem

开发者 https://www.devze.com 2023-02-05 20:04 出处:网络
I am using a standard QGraphicsView and QGraphicsScene, but I have subclassed a QGraphicsPixmapItem to my own ImagePixmapItem for the purposes of being able to use the mouse events on the ImagePixmapI

I am using a standard QGraphicsView and QGraphicsScene, but I have subclassed a QGraphicsPixmapItem to my own ImagePixmapItem for the purposes of being able to use the mouse events on the ImagePixmapItem.

What do I need to do to be able to capture those events? I have overridden a couple of functions like so:

   void ImagePixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event){

       qDebug("hello");

   }

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

}

However, neither of those qDebug statements ever print to console. Do I need to change anything else about my scene or graphicsview? In the constructor for the ImagePixmapItem, i did add a couple of things:

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

Bu开发者_如何学Got it has done no good.


You can use event filters.

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

I would also add that for your original example, be sure to enable mouse tracking on the widgets in question.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号