Note that there are some explanatory texts on larger screens.

plurals
  1. POReordering items QAbstractListModel -- dropMimeData never called
    primarykey
    data
    text
    <p>I have class <code>MyListModel</code>, which inherits from QAbstractListModel that is being displayed using a QListView or custom subclass. I would like each item in the list to be editable, and for the user to be able to drag and drop to rearrange the order of the items (I'm not actually convinced a QListView is the way to go, but it looked like it would work without too much trouble. Oh well.). </p> <p>The items are draggable, but display the little crossed out circle, meaning I can't drop anything there.</p> <p>I tried everything suggested in <a href="https://stackoverflow.com/questions/7355510/qt-what-needs-to-be-done-for-a-custom-model-to-enable-drop">this question</a>, and nothing seems to work. The <code>dropMimeData</code> function is never called, although the <code>mimeData</code> seems to produce my placeholder data.</p> <p>I don't think I'm doing anything wrong in the other classes, so I'm just putting the model class in this post. The spacing may be a bit funky.</p> <p>Thank you for any help.</p> <p>My Model:</p> <pre><code>class MyListModel(QtCore.QAbstractListModel): def __init__(self, parent, items=None): super(self.__class__, self).__init__() print "initiating MyListModel" self.parent = parent self._items = list() for thing in items: self._items.append(thing) self.setSupportedDragActions(QtCore.Qt.MoveAction) def rowCount(self, parent = QtCore.QModelIndex()): return len(self._items) def data(self, index, role = QtCore.Qt.DisplayRole): if (role == QtCore.Qt.DisplayRole) or (role == QtCore.Qt.EditRole): return self._items[index.row()].name else: return def setData(self, index, value, role=QtCore.Qt.EditRole): print "Setting Data", value, "at", index.row(), if role == QtCore.Qt.EditRole: print "EditRole" self._items[index.row()].name = value return True elif role == QtCore.Qt.DisplayRole: print "DisplayRole" print role return False else: print "other" print role return False def flags(self, index): return (QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsDragEnabled | QtCore.Qt.ItemIsDropEnabled) def insertRows(self, row, count, parent): print "insertRows" self.beginInsertRows(parent, row, (row + (count - 1))) #self._items.insert(row, object) self.endInsertRows() return True def supportedDropActions(self): print "supportedDrop" return (QtCore.Qt.MoveAction | QtCore.Qt.CopyAction) def supportedDragActions(self): print "supportedDraw" return (QtCore.Qt.MoveAction | QtCore.Qt.CopyAction) def mimeData(self, indexes): print "mimeData at (", indexes[0].row(), ",", indexes[0].column(), ")", self.old_index = indexes[0].row() old_stuff = pickle.dumps(self._items[indexes[0].row()]) print type(old_stuff) mimeData = QtCore.QMimeData() mimeData.setText(old_stuff) return mimeData def dropMimeData(self, data, action, row, column, parent): print "dropMimeData" ''' self.beginInsertRows(parentIndex, row, row) if action == QtCore.Qt.IgnoreAction: return True if data.hasText(): print data return False ''' def mimeTypes(self): print 'mimeTypes' return list("text/plain") </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload