Pyqt Mouse MouseButtonDblClick事件

时间:2013-10-08 12:15:50

标签: python pyqt mouseevent

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

这是一个例子。我想要的是,如果发生双击鼠标,只有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_())

提前谢谢!

孙燕姿

2 个答案:

答案 0 :(得分:4)

这是一种黑客攻击,但它应该可以解决问题。 此外,我使用新式信号而不是您的事件过滤器,您应该考虑。 这里,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_())

答案 1 :(得分:2)

您应该查看Double-Click-Capturing上的此帖子。

计时器可以完成这项工作。但是,无关的单击和双击可能是一个坏主意(请参阅Bill's answer区分Qt中的单击和双击事件”)。