【问题标题】:No mouse press/release events on mouse double click [duplicate]鼠标双击时没有鼠标按下/释放事件[重复]
【发布时间】:2018-10-20 16:40:33
【问题描述】:

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

【问题讨论】:

    标签: python pyqt pyqt5 pyside pyside2


    【解决方案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_())
    

    【讨论】:

    • qApp 导入错误,应该是super().mouseReleaseEvent(e)。
    • 谢谢。为什么qApp 导入错误?
    • 它应该只在QtWidgets 模块中,而不是QtCore。 PyQt5、PyQt4 和 PySide 在 QtCore 中没有 qApp 和 qt docs for qApp 表明 qApp 应该只返回一个 QApplication (这显然是一个小部件,所以它没有意义QtCore)。根据问题PYSIDE-571,似乎PySide2 已决定与Qt5 不兼容。我已经在那里发表评论质疑该决定,如果有任何回应,我会报告。
    猜你喜欢
    • 1970-01-01
    • 2021-11-20
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-05
    • 2013-06-29
    • 2019-02-09
    相关资源
    最近更新 更多