区分单击和双击在pyside [英] Differentiate single click from double click in pyside
问题描述
我尝试在Pyside中实现如何区分mouseReleaseEvent和mousedoubleClickEvent在QGrapnhicsScene ,但我不得不添加一个crufty标志,以防止显示单击后的第二个按钮释放在双点击。
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.
有更好的方法吗?
import sys
from PySide import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
"""an attempt to implement
http://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_())
推荐答案
好吧,你已经发现,原来的描述是不完整的。
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:您可以通过使用 QtGui.qApp.doubleClickInterval 的定时器时间间隔。
PS: you could slightly improve your example by using QtGui.qApp.doubleClickInterval for the timer interval.
这篇关于区分单击和双击在pyside的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!