我正在尝试创建一个列表小部件,其中 uesr 可以通过在小部件中拖动元素来重新排列元素,使用PySide-1.2.2
/Qt-4.8.7
这很简单,很简单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
周围,但我不能drop
。根据文档,设置数据模型supportedDragActions
并返回正确的flags
. 我也启用了drag
,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()
我想QAbstractListModel
我需要重写一些方法来实际接收/接受 drop 事件。但是哪一个?