重新排序项目QAbstractListModel - 从未调用dropMimeData

时间:2011-12-28 22:27:26

标签: qt pyside

我有类MyListModel,它继承自使用QListView或自定义子类显示的QAbstractListModel。我希望列表中的每个项目都是可编辑的,并且用户能够拖放以重新排列项目的顺序(我实际上并不相信QListView是要走的路,但它看起来像它没有太多麻烦就行。哦。好吧。

这些物品是可拖动的,但是显示出一个小的划掉的圆圈,这意味着我不能丢弃任何东西。

我尝试了this question中建议的所有内容,似乎没有任何效果。永远不会调用dropMimeData函数,尽管mimeData似乎会生成我的占位符数据。

我不认为我在其他类中做错了什么,所以我只是把模型类放在这篇文章中。间距可能有点时髦。

感谢您的帮助。

我的模特:

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")

2 个答案:

答案 0 :(得分:0)

这已经晚了,但你也应该设置阻力;在您的视图中放置模式。

class MyListView(QListView):
def __init__(self, parent=None):
    QListView.__init__(self, parent)

    self._model = MyListModel(self)
    self.setModel(self._model)

    self.setDragDropMode(QAbstractItemView.InternalMove)

Documentation

答案 1 :(得分:0)

mimeData方法中,尝试将mimeData = QtCore.QMimeData()更改为mimeData = super(MyListModel, self).mimeData(indexes)

我相信mimeData对象的生命周期存在问题,如果您自己创建它,它会很快被删除。如果你这样做,它似乎有正确的寿命。不过,这只是猜测。