Python 在pyside中区分单击和双击

Python 在pyside中区分单击和双击,python,click,pyside,double-click,Python,Click,Pyside,Double Click,我曾尝试在Pyside中实现中描述的方法,但我必须添加一个粗糙的标志,以防止在双击第二个按钮释放后显示单击 有更好的办法吗 import sys from PySide import QtGui, QtCore class Example(QtGui.QWidget): def __init__(self): super(Example, self).__init__() """an attempt to implement http

我曾尝试在Pyside中实现中描述的方法,但我必须添加一个粗糙的标志,以防止在双击第二个按钮释放后显示单击

有更好的办法吗

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_())

正如你所发现的,原来的描述是不完整的

它提供了一种解决方案,用于区分双击和单击的第一次单击,而不是双击和单击的第二次单击

区分第二次单击的最简单解决方案是使用标志

PS:您可以使用for the timer interval稍微改进您的示例