简体   繁体   中英

pyqt4 mouse release event not working

Im trying to track a mousepress and mouserelease event but the mouse release event it not recognized.

import sys, os

from PyQt4.QtCore import *
from PyQt4.QtGui import *



class Widget(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)

        self.btn = QPushButton("Add Line")

        self.gv = QGraphicsView()
        self.scene = QGraphicsScene(self)
        self.gv.setScene(self.scene)
        self.gv.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform)

        lay = QHBoxLayout(self)
        lay.addWidget(self.btn)
        lay.addWidget(self.gv)
        script_dir = sys.path[0]

        image_loc = os.path.dirname(script_dir) + '/testproject/configs/files/lena.png'
        print image_loc
        #self.p_item = self.scene.addPixmap(QPixmap("lena.png"))
        self.p_item = self.scene.addPixmap(QPixmap(image_loc))

        self.btn.clicked.connect(self.add_line)

    def add_line(self, event):
        import pprint
        print pprint.pprint(dir(event))
        pass

    def mousePressEvent(self, QMouseEvent):
        print 'mouse press event = ', QMouseEvent.pos()

    def mouseReleaseEvent(self, QMouseEvent):
        print 'mouse release event = ', QMouseEvent.pos()
        # cursor = QCursor()
        # print 'mouse release event = ', cursor.pos()


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

In Qt the events are passed from the parents to the children, but not vice versa. In your case the event mousePressEvent starts in Widget, then you can move to QPushButton or QGraphicsView , let's assume it is in this case that it falls in QGraphicsView then it happens to your child widget called viewport() , this will be done until some widget accept the event, the same must happen with mouseReleaseEvent , but for an object to receive the mouseReleaseEvent must have accepted the mousePressEvent event, and the only one that has done so is the viewport() of QGraphicsView . The solution is to install an event filter to the viewport() of QGraphicsView as shown below.

class Widget(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)    
        self.btn = QPushButton("Add Line")

        self.gv = QGraphicsView()
        self.scene = QGraphicsScene(self)
        self.gv.setScene(self.scene)
        self.gv.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform)

        lay = QHBoxLayout(self)
        lay.addWidget(self.btn)
        lay.addWidget(self.gv)
        script_dir = sys.path[0]

        image_loc = os.path.dirname(script_dir) + '/testproject/configs/files/lena.png'
        print(image_loc)
        #self.p_item = self.scene.addPixmap(QPixmap("lena.png"))
        self.p_item = self.scene.addPixmap(QPixmap(image_loc))

        self.btn.clicked.connect(self.add_line)

        self.gv.viewport().installEventFilter(self) # <----

    def add_line(self, event):
        import pprint
        print(pprint.pprint(dir(event)))

    def eventFilter(self, obj, event):
        if obj is self.gv.viewport():
            if event.type() == QEvent.MouseButtonPress:
                print('mouse press event = ', event.pos())
            elif event.type() == QEvent.MouseButtonRelease:
                print('mouse release event = ', event.pos())

        return QWidget.eventFilter(self, obj, event)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM