PDA

View Full Version : mouseWheel event not going to QGraphicsItem



spacedman
17th March 2009, 21:03
I have a custom QGraphicsScene with a custom item on it. Both scene and item have wheelEvent(self,event) handlers, but only the scene handler gets called. The documentation claims items under the cursor get the event first, and it only goes to the scene if they all ignore it. That's not what seems to be happening.

If I remove the scene's eventWheel handler then the item gets wheel events.

Here's some PyQt4 code that shows it. As it is, only the scene gets wheel events. Comment out MyScene's wheelEvent handler and then MyItem gets them. Same for everyone else? This is on Ubuntu Hardy.



from PyQt4.QtCore import *
from PyQt4.QtGui import *

class MyItem(QGraphicsRectItem):
def __init__(self,x,y,w,h,parent):
QGraphicsRectItem.__init__(self,w/2.0,h/2.0,w,h,parent)
self.setPos(x,y)
self.setPen(QPen(Qt.NoPen))
self.setBrush(QBrush(QColor(Qt.black)))
def wheelEvent(self,event):
print "Item got a wheel"

class MyScene(QGraphicsScene):
def __init__(self):
QGraphicsScene.__init__(self)
def wheelEvent(self,event):
print "scene got a wheel"

app = QApplication([])

dialog = QDialog()
view = QGraphicsView()
scene = MyScene()

layout = QVBoxLayout()
layout.addWidget(view)
dialog.setLayout(layout);
m1 = MyItem(0,0,100,100,None)
scene.addItem(m1)
view.setScene(scene)

dialog.show()

app.exec_()