[PyQt] drag & drop issue with ItemViews on OSX

Phil Thompson phil at riverbankcomputing.com
Tue Mar 13 18:12:24 GMT 2012


On Tue, 13 Mar 2012 17:25:10 +0000, Jugdish <jugdizh at gmail.com> wrote:
> I've run into what looks to be a bug with drag/drop and TreeViews on
OSX,
> which appears to be specific to PyQt, as the same issue does goes away
when
> running equivalent C++ code against the same version of Qt.
> 
> When dragging from one TreeView to another, as soon as one of the
> QDragMoveEvents gets ignored as a result of hovering over an index that
> doesn't accept drops, all subsequent QDragMoveEvents will then be
rejected
> as well, regardless of where you're hovering. It looks like what's
> happening is as soon as event.ignore() is called for the first time on a
> QDragMoveEvent, the following QDragMoveEvents all have their
dropAction()
> set to 0, whereas it was non-zero before. With a wiped dropAction, the
> target view rejects them all.
> 
> Below a simple example to demonstrate the problem. For what it's worth,
it
> seems the issue is specific to PyQt and OSX, as I have tested it on the
> following setups:
> 
> OSX 10.6.7, Qt 4.6.3, PyQt 4.7.6 => BUG HAPPENS
> OSX 10.6.7, Qt 4.6.3 only (C++ code) => no bug
> Linux Fedora 8, Qt 4.6.2, PyQt 4.7 => no bug
> 
> #######################################################
> from PyQt4 import QtCore, QtGui
> 
> a = QtGui.QApplication([])
> 
> mainWidget = QtGui.QWidget()
> 
> # set up left-hand model/view (drop target)
> leftView = QtGui.QTreeView(mainWidget)
> leftModel = QtGui.QStandardItemModel(mainWidget)
> parentItem = leftModel.invisibleRootItem()
> parentItem.setDropEnabled(False)
> for i in xrange(5):
>     item = QtGui.QStandardItem()
>     # enable dropping on everything except 3rd item
>     if i == 2:
>         item.setDropEnabled(False)
>         item.setText("item %d (UNDROPPABLE)" % i)
>     else:
>         item.setDropEnabled(True)
>         item.setText("item %d (DROPPABLE)" % i)
>     parentItem.appendRow(item)
> leftView.setModel(leftModel)
> leftView.setAcceptDrops(True)
> 
> # set up right-hand model/view (drag source)
> rightView = QtGui.QTreeView(mainWidget)
> rightModel = QtGui.QStandardItemModel(mainWidget)
> item = QtGui.QStandardItem("DRAG ME")
> item.setDragEnabled(True)
> rightModel.invisibleRootItem().appendRow(item)
> rightView.setModel(rightModel)
> rightView.setDragEnabled(True)
> rightView.setDefaultDropAction(QtCore.Qt.MoveAction)
> 
> # layout the views
> layout = QtGui.QHBoxLayout()
> layout.addWidget(leftView)
> layout.addWidget(rightView)
> mainWidget.setLayout(layout)
> mainWidget.show()
> 
> a.exec_()

Seems to work fine for me (if I'm running the test properly)...

OS/X 10.7.3, Qt 4.8.0, PyQt 4.9.1

Phil


More information about the PyQt mailing list