0

我想同时使用这两个事件:mouseDoubleClickmouseReleaseEvent. 但是有一个问题:即使双击鼠标也总是触发后一个事件。

4

1 回答 1

1

答案在下面。请发布建议以帮助我改进此代码。

from PySide2.QtCore import qApp, QTimer
from PySide2.QtWidgets import QApplication, QWidget


class Widget(QWidget):
    def __init__(self):
        super().__init__()
        self.timer = QTimer(self)
        self.timer.setSingleShot(True)
        self.timer.timeout.connect(self.single_click)
        self.double_click_interval = qApp.doubleClickInterval()

    def mouseReleaseEvent(self, e):
        if not self.timer.isActive():
            self.timer.start(self.double_click_interval)
        else:
            self.timer.stop()
            self.double_click()
        super().mouseReleaseEvent(e)

    def single_click(self):
        print("single")

    def double_click(self):
        print("double")


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)

    w = Widget()
    w.show()
    w.raise_()

    sys.exit(app.exec_())
于 2018-05-10T11:55:43.270 回答