python - QWidget::mouseMoveEvent not firing when cursor over child widget -
i'm trying capture cursor coordinates mouse moved within qwidget
reimplementing qwidget::mousemoveevent()
. mouse tracking enabled, mouse move events generated move cursor around main widget. however, when cursor placed on child widget mouse move events cease fire.
mouse press/release events work while cursor on same child widget, , move events firing correctly if mouse button held. i've tried enabling mouse tracking on children too, doesn't seem make difference. how can trigger mouse move events when mouse on child widget?
here's minimum working example demonstrates problem:
import sys pyqt4 import qtcore, qtgui class mywindow(qtgui.qwidget) : def __init__(self): qtgui.qwidget.__init__(self) tabs = qtgui.qtabwidget() tab1 = qtgui.qwidget() tab2 = qtgui.qwidget() tabs.addtab(tab1, "tab 1") tabs.addtab(tab2, "tab 2") layout = qtgui.qvboxlayout() layout.addwidget(tabs) self.setlayout(layout) self.setmousetracking(true) def mousemoveevent(self, event): print 'mousemoveevent: x=%d, y=%d' % (event.x(), event.y()) app = qtgui.qapplication(sys.argv) window = mywindow() window.setfixedsize(640, 480) window.show() sys.exit(app.exec_())
when mouse moved outside of qtabwidget
mouse coordinates printed expected. inside of nothing happens unless mouse button held.
the problem code need enable mouse tracking widgets explicitly. can iterating on children of main widget, , calling setmousetracking(true)
each of them. here i've overridden setmousetracking()
that:
import sys pyqt4 import qtcore, qtgui class mywindow(qtgui.qwidget) : def __init__(self): qtgui.qwidget.__init__(self) tabs = qtgui.qtabwidget() tab1 = qtgui.qwidget() tab2 = qtgui.qwidget() tabs.addtab(tab1, "tab 1") tabs.addtab(tab2, "tab 2") layout = qtgui.qvboxlayout() layout.addwidget(tabs) self.setlayout(layout) self.setmousetracking(true) def setmousetracking(self, flag): def recursive_set(parent): child in parent.findchildren(qtcore.qobject): try: child.setmousetracking(flag) except: pass recursive_set(child) qtgui.qwidget.setmousetracking(self, flag) recursive_set(self) def mousemoveevent(self, event): print 'mousemoveevent: x=%d, y=%d' % (event.x(), event.y()) app = qtgui.qapplication(sys.argv) window = mywindow() window.setfixedsize(640, 480) window.show() sys.exit(app.exec_())
Comments
Post a Comment