Skip to main content
 首页 » 编程设计

python之鼠标点击是否穿透Qwidgets到pyside中的app

2025年05月04日28lyj

我正在尝试检测我的 gui 上的鼠标点击,下面的代码允许检测 1 层 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_()) 

但是,如果我在 Qwidget 和 Qwidget 中嵌入了 Qwidget,则鼠标点击不会穿透应用程序。

不仅如此,令人困惑的是,当我将事件过滤器安装到内部小部件时,鼠标点击仍然根本没有被检测到。

# Widget x.1 is embedded in Widget X 
 
#    -----------------Widget x.1----------------------- 
#    |                                                | 
#    |                                                | 
#    | ----------------------  ---------------------| | 
#    | |                    |  |                    | | 
#    | |   Widget  x.1.1 |  |  |   Widget x.1.2     | 
#    | |                    |  |                    | | 
#    | ----------------------  ---------------------| | 
#    |                                                | 
#    -------------------------------------------------- 

我的解决方案有误吗?任何建议将不胜感激。

请您参考如下方法:

这是 mouse event propagation 的预期行为在 Qt 中。

QLabel 和 QPushButton 默认接受鼠标事件,使用它们并且不让它们传播到父级。您可以继承您的 QWidget 并覆盖鼠标交互方法而不接受事件,然后它们将传播。您可以在所有内容之上放置一个透明的捕获鼠标事件小部件。

或者您可以使用有效的事件过滤器。我运行程序,MouseDetector 运行良好(Windows 7、Python 2.7、PySide 1.2.2):

mouse pressed <PySide.QtGui.QPushButton object at 0x00000000038A0A88> 
mouse pressed <PySide.QtGui.QLabel object at 0x00000000038A0908> 
mouse pressed <__main__.MainWindow object at 0x00000000038A0888> 

QWidget 高于一切,捕捉鼠标事件很容易完成。例如:

from PySide import QtCore, QtGui 
 
class ClickableWidget(QtGui.QWidget): 
 
    clicked = QtCore.Signal(QtGui.QMouseEvent) 
 
    def __init__(self, *args, **kwargs): 
        super().__init__(*args, **kwargs) 
 
    def mousePressEvent(self, event): 
        self.clicked.emit(event) 
        event.ignore() 
 
def beep(): 
    print('button clicked') 
 
def dup(): 
    print('catcher clicked') 
 
app = QtGui.QApplication([]) 
window = QtGui.QWidget() 
button = QtGui.QPushButton('Click me', window) 
button.clicked.connect(beep) 
catcher = ClickableWidget(window) 
catcher.clicked.connect(dup) 
catcher.raise_() 
 
window.show() 
 
app.exec_() 

这里按钮是可见的但不能被点击,因为另一个QWidget(默认是透明的)在它上面(raise_(​​))。实际上,即使没有发出信号,只是在上方放置一个小部件,它也会捕获所有鼠标事件,因为事件仅传播给父级(并且捕获小部件不是此处按钮的子级)。另一种方式(忽略事件)有点棘手。参见 Qt - top level widget with keyboard and mouse event transparency?例如。