gpt4 book ai didi

python - Pyqt 鼠标 MouseButtonDblClick 事件

转载 作者:行者123 更新时间:2023-12-01 22:38:35 28 4
gpt4 key购买 nike

我在区分单击鼠标事件和双击鼠标事件时遇到了一些麻烦。我已经创建了一个事件过滤器,但双击鼠标也会返回一个信号。在我的代码中,我必须将两个事件分开以连接到不同的函数。谁能建议我怎么做?

这是一个例子。我想要的是,如果发生双击鼠标,只有 MouseButtonDblClick 应该发出信号,而不是 LeftButton 和 MouseButtonDblClick:

    # coding: utf-8
import sys

from PyQt4 import QtCore, QtGui


class MyDialog(QtGui.QDialog):
def __init__(self, parent=None):
super(MyDialog, self).__init__(parent)

self.button1 = QtGui.QPushButton("Button 1")
self.button2 = QtGui.QPushButton("Button 2")

hbox = QtGui.QHBoxLayout()
hbox.addWidget(self.button1)
hbox.addWidget(self.button2)
self.setLayout(hbox)

self.button1.installEventFilter(self)

def eventFilter(self, obj, event):

if event.type() == QtCore.QEvent.MouseButtonPress:
if event.button() == QtCore.Qt.LeftButton:
#If image is left clicked, display a red bar.
print 'one left'
elif event.button() == QtCore.Qt.RightButton:
print 'one right'
elif event.type() == QtCore.QEvent.MouseButtonDblClick:
#If image is double clicked, remove bar.
print 'two'

return super(MyDialog, self).eventFilter(obj, event)


if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
w = MyDialog()
w.show()
sys.exit(app.exec_())

提前致谢!

孙燕姿

最佳答案

这有点像 hack,但应该可以解决问题。另外,我使用了新型信号而不是您的事件过滤器,这是您应该考虑的事情。此处,ClickHandler 类计算第一次点击与其计时器超时事件之间的点击次数。

from PyQt4 import QtCore, QtGui

class ClickHandler():
def __init__(self, time):
self.timer = QtCore.QTimer()
self.timer.setInterval(time)
self.timer.setSingleShot(True)
self.timer.timeout.connect(self.timeout)
self.click_count = 0

def timeout(self):
if self.click_count == 1:
print('Single click')
elif self.click_count > 1:
print('Double click')
self.click_count = 0

def __call__(self):
self.click_count += 1
if not self.timer.isActive():
self.timer.start()


class MyDialog(QtGui.QDialog):
def __init__(self, parent=None):
super(MyDialog, self).__init__(parent)

self.button1 = QtGui.QPushButton("Button 1")
hbox = QtGui.QHBoxLayout()
hbox.addWidget(self.button1)
self.setLayout(hbox)

self.click_handler = ClickHandler(300)
self.button1.clicked.connect(self.click_handler)

if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = MyDialog()
w.show()
sys.exit(app.exec_())

编辑:第二个更清晰的版本,带有处理左右点击信号的 CustomButton 类:

from PyQt4 import QtCore, QtGui

class CustomButton(QtGui.QPushButton):

left_clicked= QtCore.pyqtSignal(int)
right_clicked = QtCore.pyqtSignal(int)

def __init__(self, *args, **kwargs):
QtGui.QPushButton.__init__(self, *args, **kwargs)
self.timer = QtCore.QTimer()
self.timer.setInterval(250)
self.timer.setSingleShot(True)
self.timer.timeout.connect(self.timeout)
self.left_click_count = self.right_click_count = 0

def mousePressEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
self.left_click_count += 1
if not self.timer.isActive():
self.timer.start()
if event.button() == QtCore.Qt.RightButton:
self.right_click_count += 1
if not self.timer.isActive():
self.timer.start()

def timeout(self):
if self.left_click_count >= self.right_click_count:
self.left_clicked.emit(self.left_click_count)
else:
self.right_clicked.emit(self.right_click_count)
self.left_click_count = self.right_click_count = 0


class MyDialog(QtGui.QDialog):
def __init__(self, parent=None):
super(MyDialog, self).__init__(parent)
self.button1 = CustomButton("Button 1")
hbox = QtGui.QHBoxLayout()
hbox.addWidget(self.button1)
self.setLayout(hbox)
self.button1.left_clicked[int].connect(self.left_click)
self.button1.right_clicked[int].connect(self.right_click)

def left_click(self, nb):
if nb == 1: print('Single left click')
else: print('Double left click')

def right_click(self, nb):
if nb == 1: print('Single right click')
else: print('Double right click')


if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = MyDialog()
w.show()
sys.exit(app.exec_())

关于python - Pyqt 鼠标 MouseButtonDblClick 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19247436/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com