区分单击在pyside中双击

时间:2014-03-16 22:50:21

标签: python click pyside double-click

我试图在Pyside中实现How to distinguish between mouseReleaseEvent and mousedoubleClickEvent on QGrapnhicsScene中描述的方法,但是我必须添加一个crufty标志,以防止它在双击第二个按钮释放后显示单击。

有更好的方法吗?

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 个答案:

答案 0 :(得分:1)

嗯,正如您所发现的那样,原始描述不完整。

它提供了一个解决方案,用于区分双击和单击的第一次点击,但不是第二次点击双击和单击-Click。

区分第二次点击的最简单方法是使用标记。

PS:您可以使用QtGui.qApp.doubleClickInterval作为计时器间隔,稍微改善您的示例。