2019-02-15 双击屏幕的事件过滤

2019-02-26  本文已影响0人  gongdiwudu

双击view对象后,在manwin上响应该事件;需要过滤出这个事件的发生,以及双击的地点。

1)生成 QtWidgets.QGraphicsView并使它居于mainWindow中间

2)在该view上面,安装事件的过滤器self.graphicsView.viewport().installEventFilter(self)

3)在manWin上,超载eventFilte函数

def eventFilter(self, obj, event):

    if objis self.graphicsView.viewport():

        if event.type() == QtCore.QEvent.MouseButtonDblClick:

            self.func(event)  此处建立超载的操作函数

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

实验代码:

from PyQt5import QtCore, QtWidgets

class MainWindow( QtWidgets.QMainWindow ):

        def __init__(self, parent=None):

                super(MainWindow,self).__init__(parent)

                self.graphicsView= QtWidgets.QGraphicsView()

                self.setCentralWidget(self.graphicsView)

                self.graphicsView.viewport().installEventFilter(self)

                self.in_1 =10;self.in_2 =20

        def eventFilter(self, obj, event):

                if  obj  is  self.graphicsView.viewport():

                        if event.type() == QtCore.QEvent.MouseButtonDblClick:

                                self.func(event)

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

        def func(self, event):

                print(event.pos(),self.in_1,self.in_2)

if __name__ =='__main__':

        import sys

        app = QtWidgets.QApplication(sys.argv)

        w = MainWindow()

        w.show()

        sys.exit(app.exec_())

上一篇下一篇

猜你喜欢

热点阅读