[PySide] Double click handling or retaining ownership of mouse events

Marco Grubert grubertm at hotmail.com
Wed Apr 8 02:01:34 CEST 2020


Hello,

I am trying to handle single and double-click events differently in a QToolButton.
>From what I have read in forums, Qt does not filter out single click events when a double click occurs, in other words there will always be a mousePressEvent emitted even if it's actually a double-click. The recommended approach in C++ appears to be launching a timer and deferring single click handling.

a) If there is a better approach, please let me know!
b) I tried the implementation below, but in the timerExpired function I get the dreaded "Internal C++ object (PySide2.QtGui.QMouseEvent) already deleted" error. I guess storing it in a member variable is insufficient?

class QToolButtonTest(QToolButton):
    def __init__(self, parent = None):
        super().__init__(parent)
        self.timer = None

    def cancelTimer(self):
        if self.timer:
            self.timer.stop()
        self.timer = None
        self.mousePressData = None

    def timerExpired(self):
        super().mousePressEvent(self.mousePressData)
        self.cancelTimer()

    def mousePressEvent(self, event):
        if not self.timer:
            self.mousePressData = event
            self.timer = QTimer(self)
            self.timer.timeout.connect(self.timerExpired)
            self.timer.setSingleShot(True)
            self.timer.start(1000)

    def mouseDoubleClickEvent(self, event):
        self.cancelTimer()
        super().mouseDoubleClickEvent(event)


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20200408/f67dd764/attachment.html>


More information about the PySide mailing list