I am trying to detect a mouse click on my gui, and the following code allows detection of mouse click in 1 layer of Qwidget
import sys
from PySide import QtGui, QtCore
class MouseDetector(QtCore.QObject):
def eventFilter(self, obj, event):
if event.type() == QtCore.QEvent.MouseButtonPress:
print 'mouse pressed', obj
return super(MouseDetector, self).eventFilter(obj, event)
class MainWindow(QtGui.QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
layout = QtGui.QHBoxLayout()
layout.addWidget(QtGui.QLabel('this is a label'))
layout.addWidget(QtGui.QPushButton('Button'))
self.setLayout(layout)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mouseFilter = MouseDetector()
app.installEventFilter(mouseFilter)
main = MainWindow()
main.show()
sys.exit(app.exec_())
However if I have a Qwidget embedded within Qwidget and within a Qwidget, the mouse clicks do not penetrate through the app.
Not only that, what is confusing is that when I install Event filter to the inner widget, the mouse clicks still don't get detected at all.
# Widget x.1 is embedded in Widget X
# -----------------Widget x.1-----------------------
# | |
# | |
# | ---------------------- ---------------------| |
# | | | | | |
# | | Widget x.1.1 | | | Widget x.1.2 |
# | | | | | |
# | ---------------------- ---------------------| |
# | |
# --------------------------------------------------
Am I approaching the solution wrong? Any advice would be much appreciated.