在 PyQt5 小部件中检测单击鼠标 - 缺少 mouseClickEvent 函数

Detect single mouse click in PyQt5 Widgets - missing mouseClickEvent function

我想在 PyQt5 中识别 QWidget 上的鼠标单击。在 docs 中有 mouseDoubleClickEventmousePressEvent 但没有 mouseClickEvent 单击的功能。我如何获得该功能?谢谢

顺便说一句,我注意到 PyQtGraph 确实有一个 mouseClickEvent 函数。

Qt 本身不提供“点击”事件,您必须基于 mousePressEventmouseReleaseEvent 通过检查释放是否发生在几何图形中来实现这样的功能小部件(并且可能按下并释放了正确的鼠标按钮)。

class ClickWidget(QWidget):
    pressPos = None
    clicked = pyqtSignal()

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.pressPos = event.pos()

    def mouseReleaseEvent(self, event):
        # ensure that the left button was pressed *and* released within the
        # geometry of the widget; if so, emit the signal;
        if (self.pressPos is not None and 
            event.button() == Qt.LeftButton and 
            event.pos() in self.rect()):
                self.clicked.emit()
        self.pressPos = None