I want to use both events: mouseDoubleClick
and mouseReleaseEvent
. But there is an issue: The latter event is always triggered even when one double clicks the mouse.
Asked
Active
Viewed 762 times
1 Answers
1
Answer is here below. Please post suggestions to help me improve this code.
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_())

Matphy
- 1,086
- 13
- 21
-
2The `qApp` import is wrong, and it should be `super().mouseReleaseEvent(e)`. – ekhumoro May 10 '18 at 17:05
-
Thank you. Why is `qApp` import wrong? – Matphy May 11 '18 at 08:12
-
1It should really only be in the `QtWidgets` module, not `QtCore`. PyQt5, PyQt4 and PySide do not have `qApp` in `QtCore` and the [qt docs for qApp](https://doc.qt.io/qt-5/qapplication.html#qApp) show that `qApp` should only ever return a `QApplication` (which is obviously a widget, so it makes no sense for it to be in `QtCore`). According to issue [PYSIDE-571](https://bugreports.qt.io/browse/PYSIDE-571?jql=text%20~%20qapp), it seems that PySide2 has decided to be incompatible with Qt5. I have made a comment there to question that decision, and will report back if there is any response. – ekhumoro May 11 '18 at 14:21