Can QWidget detect mouse events on behalf of a QLineEdit?

倾然丶 夕夏残阳落幕 提交于 2019-12-02 03:34:49

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!