[PyQt] PyQt5 confusion between mousePressEvent and mouseDoubleClickEvent
Vincent Vande Vyvre
vincent.vande.vyvre at telenet.be
Fri Apr 29 15:32:56 BST 2016
I'm porting an application from PyQt4 to PyQt5, on some widgets I need
to handle the mousePressEvent and the mouseDoubleClickEvent with
different actions.
So, I use a QTimer for that, I thinks it's the common usage.
With PyQt4 that's works fine but on PyQt5 in case of double click the
mousePressEvent is emitted twice.
These codes demonstrate the problem:
----------------------------------------------------
from PyQt4 import QtCore, QtGui
is_double_clicked = False
def on_click(event):
print 'Click'
if event.button() == 1:
timer = QtCore.QTimer()
timer.singleShot(300, on_item_clicked)
timer.start()
def on_double_click(event):
global is_double_clicked
print 'Double click'
is_double_clicked = True
def on_item_clicked():
global is_double_clicked
print 'After timer'
if is_double_clicked:
is_double_clicked = False
app = QtGui.QApplication([])
w = QtGui.QWidget()
l = QtGui.QLabel('Label', w)
l.mousePressEvent = on_click
l.mouseDoubleClickEvent = on_double_click
w.show()
app.exec_()
--------------------------------------------------
from PyQt5 import QtCore, QtWidgets
is_double_clicked = False
def on_click(event):
print('Click')
if event.button() == 1:
timer = QtCore.QTimer()
timer.singleShot(300, on_item_clicked)
timer.start()
def on_double_click(event):
global is_double_clicked
print('Double click')
is_double_clicked = True
def on_item_clicked():
global is_double_clicked
print('After timer')
if is_double_clicked:
is_double_clicked = False
app = QtWidgets.QApplication([])
w = QtWidgets.QWidget()
l = QtWidgets.QLabel('Label', w)
l.mousePressEvent = on_click
l.mouseDoubleClickEvent = on_double_click
w.show()
app.exec_()
-------------------------------------------------
Vincent
More information about the PyQt
mailing list