I am trying to capture the coordinates of a cursor when a mouse moves within a QWidget , overriding QWidget::mouseMoveEvent() . With mouse tracking turned on, mouse movement events are generated when I move the cursor around the main widget. However, when the cursor is placed over the child widgets, mouse movement events stop working.
Mouse click and release events work when the cursor is over the same child widgets, and event movement works correctly if the mouse button is held down. I tried to enable mouse tracking for kids as well, but it doesn't seem to make any difference. How can I fire mouse movement events when the mouse is over child widgets?
Here is a minimal working example demonstrating the problem:
import sys from 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 the mouse moves outside the QTabWidget , the coordinates of the mouse are printed as expected. Nothing happens inside this if the mouse button is not held.
source share