[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    [PyQt] PyQt5 confusion between mousePressEvent and mouseDoubleClickEvent
From:       Vincent Vande Vyvre <vincent.vande.vyvre () telenet ! be>
Date:       2016-04-29 14:32:56
Message-ID: 57237098.4050206 () telenet ! be
[Download RAW message or body]

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
_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic