简体   繁体   中英

intercept mouse events when creating a custom QGraphicsItem

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);

But 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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