我正在尝试使用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
,但我无法{{1}它。
根据文档,设置数据模型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()
的一些方法来实际接收/接受drop事件。但是哪一个?