QDragLeaveEvent called while mouse still in widget



  • When implementing drag and drop within a QListWidget, a spurious QDragLeaveEvent is fired while the mouse is still in the widget. If you run this program it should print mouse and drag/drop events. You may have to start a drag, then stop dragging but don't release the mouse. Then, move the mouse again.

    @
    import sys
    from PyQt4.QtGui import *
    from PyQt4.QtCore import *
    from PyQt4.QtCore import Qt as qt

    class ListView(QListWidget):
    def init(self):
    super(ListView, self).init()
    self.setAcceptDrops(True)
    def dragEnterEvent(self, e):
    print 'dragEnterEvent'
    if e.mimeData().hasFormat('text/plain'):
    e.acceptProposedAction()
    def dragLeaveEvent(self, e):
    ###############################################################
    # This gets called while the mouse is still within the widget #
    ###############################################################
    print 'dragLeaveEvent'
    def dragMoveEvent(self, e):
    print 'dragMoveEvent'
    e.acceptProposedAction()
    def dropEvent(self, e):
    print 'dropEvent'
    text = e.mimeData().text()
    item = QListWidgetItem(text)
    self.insertItem(0, item)
    e.acceptProposedAction()
    def mousePressEvent(self, e):
    print 'mousePressEvent'
    if e.button() == qt.LeftButton:
    self.downPos = e.pos()
    super(ListView, self).mousePressEvent(e)
    def mouseMoveEvent(self, e):
    print 'mouseMoveEvent'
    if e.buttons() != qt.LeftButton:
    return
    if self.currentItem() is None:
    return
    if self.itemAt(self.downPos) != self.currentItem():
    return
    dragDist = (e.pos() - self.downPos).manhattanLength()
    if dragDist < QApplication.startDragDistance():
    return
    drag = QDrag(self)
    mimeData = QMimeData()
    mimeData.setText(self.currentItem().text())
    drag.setMimeData(mimeData)
    self.takeItem(self.currentRow())
    drag.exec_(qt.MoveAction)

    if name == 'main':
    app = QApplication(sys.argv)
    v = ListView()
    v.insertItem(0, QListWidgetItem("Foo"))
    v.show()
    app.exec_()
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.