QListView中的可拖动项目

时间:2017-01-27 19:47:45

标签: model-view-controller qt4 pyside qlistview qabstractlistmodel

我正在尝试使用PySide-1.2.2 / Qt-4.8.7

创建一个列表小部件,其中uesr可以通过在小部件中拖动元素来重新排列元素

使用简单的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()

但是,我正在尝试对QListView / QAbstractListModel执行相同的操作,虽然我可以“抓住”某个项目并drag,但我无法{{1}它。 根据文档,设置数据模型drop并返回正确的supportedDragActions应该足够了。 我还启用了flagsdrag并将acceptDrops设置为DragDropMode InternalMode,但无济于事。

QListView

我想我需要覆盖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() 的一些方法来实际接收/接受drop事件。但是哪一个?

0 个答案:

没有答案