创建自定义 QGraphicsItem 时拦截鼠标事件
我正在使用标准 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);
但它没有起到任何作用。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用事件过滤器。
http://cartan.cas.suffolk.edu/qtdocs/eventsandfilters.html
我还要补充一点,对于您的原始示例,请务必在相关小部件上启用鼠标跟踪。
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.