Differentiate single click from double click in pyside

一个人想着一个人 提交于 2019-12-11 02:41:48

问题


I have tried to implement in Pyside the method described in How to distinguish between mouseReleaseEvent and mousedoubleClickEvent on QGrapnhicsScene, but I had to add a crufty flag to keep it from showing single click after the second button release in a double click.

Is there a better way?

import sys
from PySide import QtGui, QtCore

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()
        """an attempt to implement 
        https://stackoverflow.com/questions/18021691/how-to-distinguish-between-mousereleaseevent-and-mousedoubleclickevent-on-qgrapn
        main()
            connect(timer, SIGNAL(timeout()), this, SLOT(singleClick()));
        mouseReleaseEvent()
            timer->start();
        mouseDoubleClickEvent()
            timer->stop();
        singleClick()
            // Do single click behavior
        """
        self.timer = QtCore.QTimer()
        self.timer.setSingleShot(True)
        # had to add a "double_clicked" flag
        self.double_clicked = False
        self.timer.timeout.connect(self.singleClick)

        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Single click, double click')    
        self.show()

    def mouseReleaseEvent(self, event):
        if not self.double_clicked:
            self.timer.start(200)
        else:
            self.double_clicked = False

    def mouseDoubleClickEvent(self, event):
        self.timer.stop()
        self.double_clicked = True
        print 'double click'

    def singleClick(self):
        print 'singleClick'

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

回答1:


Well, as you've discovered, the original description is incomplete.

It gives a solution for distinguishing between the first click of a double-click and single-click, but not the second click of a double-click and a single-click.

The simplest solution for distinguishing the second click is to use a flag.

PS: you could slightly improve your example by using QtGui.qApp.doubleClickInterval for the timer interval.



来源:https://stackoverflow.com/questions/22444152/differentiate-single-click-from-double-click-in-pyside

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!