首页 > 解决方案 > 有拖动数据时未触发 mouseReleaseEvent?

问题描述

在我的测试演示中,mouseReleaseEventmouseMoveEvent使用QDrag. mouseReleaseEvent当我拖动某些东西时,我也想工作。

class Item2(QGraphicsItem):
    def boundingRect(self):
        return QRectF(-50, -50, 100, 100)
    
    def paint(self, p, o, w):
        p.setBrush(Qt.red)
        p.drawRect(-50, -50, 100, 100)
    
    def mousePressEvent(self, e):
        if e.button() == Qt.LeftButton:
            self.setCursor(Qt.ClosedHandCursor)
            print('press')
    
    def mouseMoveEvent(self, e):
        if QLineF(e.buttonDownScreenPos(Qt.LeftButton), e.screenPos()).length() > QApplication.startDragDistance():
            drag = QDrag(e.widget())
            
            data = QMimeData()
            data.setText('Test')
            
            drag.setMimeData(data)
            drag.exec()
            print('move')
    
    def mouseReleaseEvent(self, e):
        self.setCursor(Qt.ArrowCursor)
        print('release')

class Demo2(QGraphicsView):
    def __init__(self):
        super().__init__()
        self.scene = QGraphicsScene()
        self.scene.addItem(Item2())
        self.setScene(self.scene)

app = QApplication([])
demo = Demo2()
demo.resize(800, 800)
demo.show()
app.exec()

标签: python-3.xpyqt5

解决方案


推荐阅读