PySide-1.2.2
/を使用して、uesrがウィジェット内で要素をドラッグして要素を再配置できるリストウィジェットを作成しようとしています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
ドロップイベントを実際に受信/受け入れるためにオーバーライドする必要があるメソッドがあると思います。しかし、どれですか?