1

I'm trying to create a list-widget where the uesr can rearrange the elements by dragging them within the widget, using PySide-1.2.2/Qt-4.8.7

This is pretty simple with a simple QListWidget:

from PySide.QtCore import *
from PySide.QtGui import *
import sys

class MyMainWindow(QWidget):
 def __init__(self):
  QWidget.__init__(self, None)
  vbox = QVBoxLayout()
  v = QListWidget()
  v.addItems(["A", "BB", "CCC", "DDDD", "EEEEE"])
  v.setDragDropMode(QAbstractItemView.InternalMove)
  vbox.addWidget(v)
  self.setLayout(vbox)

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

However, I'm trying to do the same with QListView/QAbstractListModel, and while I can "grab" an item and drag it around, I cannot drop it. According to the docs it should be enough to set the data model's supportedDragActions and return the correct flags. I also enabled drag, acceptDrops and set the DragDropMode to InternalMode for the QListView, but to no avail.

from PySide.QtCore import *
from PySide.QtGui import *
import sys

class SimpleListModel(QAbstractListModel):
 def __init__(self, mlist):
  QAbstractListModel.__init__(self)
  self._items = mlist
  self.setSupportedDragActions(Qt.CopyAction | Qt.MoveAction | Qt.TargetMoveAction)
 def rowCount(self, parent = QModelIndex()):
  return len(self._items)
 def data(self, index, role = Qt.DisplayRole):
  if role == Qt.DisplayRole:
   return self._items[index.row()]
 def flags(self, index):
    if index.isValid(): 
        return Qt.ItemIsSelectable|Qt.ItemIsDragEnabled|Qt.ItemIsEnabled
    return Qt.ItemIsSelectable|Qt.ItemIsDragEnabled| \
                Qt.ItemIsDropEnabled|Qt.ItemIsEnabled

class SimpleListView(QListView):
 def __init__(self, parent = None):
  QListView.__init__(self, parent)
  self.setAcceptDrops(True)
  self.setDragEnabled(True)
  self.setDragDropMode(QAbstractItemView.InternalMove)

class MyMainWindow(QWidget):
 def __init__(self):
  QWidget.__init__(self, None)
  vbox = QVBoxLayout()
  m = SimpleListModel(["A", "BB", "CCC", "DDDD", "EEEEE"])
  v = SimpleListView()
  v.setModel(m)
  vbox.addWidget(v)
  self.setLayout(vbox)

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

I guess there is some method of the QAbstractListModel which I need to override to actually receive/accept the drop event. But which one?

umläute
  • 28,885
  • 9
  • 68
  • 122

0 Answers0