Python MousePresseEvent在子部件上不起作用?

Python MousePresseEvent在子部件上不起作用?,python,pyqt,pyqt5,Python,Pyqt,Pyqt5,所以,每次我左键点击时,我都试图打印鼠标位置,但它只在布局之外工作,我不知道如何修复它。我的GUI如下所示: 这是我的untitle.py文件 from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Dialog(object): def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(400, 300)

所以,每次我左键点击时,我都试图打印鼠标位置,但它只在布局之外工作,我不知道如何修复它。我的GUI如下所示:

这是我的untitle.py文件

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(400, 300)
        self.horizontalLayout = QtWidgets.QHBoxLayout(Dialog)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setObjectName("pushButton")
        self.verticalLayout.addWidget(self.pushButton)
        self.graphicsView = QtWidgets.QGraphicsView(Dialog)
        self.graphicsView.setObjectName("graphicsView")
        self.verticalLayout.addWidget(self.graphicsView)
        self.horizontalLayout.addLayout(self.verticalLayout)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.pushButton.setText(_translate("Dialog", "PushButton"))

mousePressEvent
事件仅在小部件要处理该信息时才会调用,因此,如果在您单击的区域中有其他子小部件,则不会收到该事件,因此您只会在顶部没有其他小部件的位置获得单击,因此,在这些情况下,解决方案是通过
eventFilter
监听事件:

import sys
from PyQt5 import QtGui, QtWidgets, QtCore
from untitled import Ui_Dialog


class AppWindow(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.show()
        for w in self.findChildren(QtWidgets.QWidget)+[self]:
            w.installEventFilter(self)

    def eventFilter(self, obj, event):
        if event.type() == QtCore.QEvent.MouseButtonPress:
            if event.buttons() & QtCore.Qt.LeftButton:
                print(obj, "global pos:", event.globalPos(), 
                            "local pos", event.pos(),
                            "position with respect to self",
                            self.mapFromGlobal(obj.mapToGlobal(event.pos())))
        return super(AppWindow, self).eventFilter(obj, event)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    w = AppWindow()
    w.show()
    sys.exit(app.exec_())

@Lindau不,在这样的情况下,只需将回答标记为正确,这样OP(提问者)就表示他已经解决了问题,请检查,以便您了解更多SO规则。啊,好的!我会检查一下旅行。谢谢你的帮助!
import sys
from PyQt5 import QtGui, QtWidgets, QtCore
from untitled import Ui_Dialog


class AppWindow(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.show()
        for w in self.findChildren(QtWidgets.QWidget)+[self]:
            w.installEventFilter(self)

    def eventFilter(self, obj, event):
        if event.type() == QtCore.QEvent.MouseButtonPress:
            if event.buttons() & QtCore.Qt.LeftButton:
                print(obj, "global pos:", event.globalPos(), 
                            "local pos", event.pos(),
                            "position with respect to self",
                            self.mapFromGlobal(obj.mapToGlobal(event.pos())))
        return super(AppWindow, self).eventFilter(obj, event)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    w = AppWindow()
    w.show()
    sys.exit(app.exec_())