I have a QWidget that contains QLabels and QLineEdits side by side.
When I click on a QLabel, I can use the mousePressEvent in QWidget. But when I click on QLineEdit, I
If you are watching the mousePressEvent()
from the main widget containing the layout, the reason you are seeing clicks for the QLabel
is because by default a QLabel
ignores the mousePressEvent
and allows it to bubble up to the next parent. Events propagate upward from child->parent.
A QLineEdit
however does need to accept and use a mouse press, to handle focus and other various actions common to a line edit widget.
So really what might be a good option for you is to use an event filter. This will allow your main widget to watch events for other widgets, without having to subclass the QLineEdit
and implement the mousePressEvent
In this example, it simply watches for any member of the layout and a mouse press, and prints, then does the default action to not change anything:
class Widget(QtGui.QWidget):
def __init__(self):
super(Widget, self).__init__()
self.layout = QtGui.QHBoxLayout(self)
self.label = QtGui.QLabel("Label")
self.line = QtGui.QLineEdit()
self.layout.addWidget(self.label)
self.layout.addWidget(self.line)
# have this widget (self) watch events for these
self.label.installEventFilter(self)
self.line.installEventFilter(self)
def mousePressEvent(self, event):
print "Main Widget Mouse Press"
super(Widget, self).mousePressEvent(event)
def eventFilter(self, obj, event):
# you could be doing different groups of actions
# for different types of widgets and either filtering
# the event or not.
# Here we just check if its one of the layout widgets
if self.layout.indexOf(obj) != -1:
if event.type() == event.MouseButtonPress:
print "Widget click", obj
# if I returned True right here, the event
# would be filtered and not reach the obj,
# meaning that I decided to handle it myself
# regardless, just do the default
return super(Widget, self).eventFilter(obj, event)
When you test this code, you will see that clicking the label causes both a print in the main widget and its event filter. But clicking on the line edit causes only the event filter print statement, because the default mousePressEvent
accepts it and does not propagate it further.