我有一个自定义的QListView:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from Diagnostics import Trace2 #writes to log file
import traceback
class ListOrderView(QListView):
itemMoved = pyqtSignal(int, int, QStandardItem) # Old index, new index, item
def __init__(self, parent=None):
try:
super(ListOrderView, self).__init__(parent)
self.setAcceptDrops(True)
self.setDragEnabled(True)
self.setDragDropMode(QAbstractItemView.InternalMove)
self.setDefaultDropAction(Qt.MoveAction)
self.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.setSelectionBehavior(QAbstractItemView.SelectRows)
self.setSelectionMode(QAbstractItemView.SingleSelection)
self.dragItem = None
self.dragRow = None
self.indexesMoved.connect(self.onIndexesMoved)
#self.installEventFilter(self)
except:
Trace2.WriteLine(str(traceback.format_exc()))
def onIndexesMoved(self, indexes):
Trace2.WriteLine("indexes were moved")
def dropEvent(self, event):
try:
super(ListOrderView, self).dropEvent(event)
self.selectionModel().setCurrentIndex(self.model().indexFromItem(self.dragItem), QItemSelectionModel.SelectCurrent)
Trace2.WriteLine("[LISTVIEW] item dropped")
Trace2.WriteLine("[LISTVIEW] current index is %d" %self.selectionModel().currentIndex().row())
Trace2.WriteLine("[LISTVIEW] current selection is %d" %self.selectionModel().selection().indexes()[0].row())
self.itemMoved.emit(self.dragRow, self.row(self.dragItem), self.dragItem)
self.dragItem = None
except:
Trace2.WriteLine(str(traceback.format_exc()))
def startDrag(self, supportedActions):
try:
self.dragItem = self.currentItem()
self.dragRow = self.row(self.dragItem)
super(ListOrderView, self).startDrag(Qt.MoveAction)
except:
Trace2.WriteLine(str(traceback.format_exc()))
def currentItem(self):
index = self.currentIndex()
item = self.model().itemFromIndex(index)
#Trace2.WriteLine("[LISTVIEW] currentItem = %s" % item.data(Qt.DisplayRole).toString())
return item
def row(self, item):
#index = self.model().indexFromItem(item)
index = self.selectedIndexes()[0]
row = index.row()
#Trace2.WriteLine("[LISTVIEW] row = %d" %row)
return row
我真的需要知道在拖放操作后项目被丢弃的位置,以便其他东西可以正确更新(我试图拖放到从未设计过的东西,大应用程序,而不是我的设计)。选择模型的当前索引和选择不遵循被删除的项目,它们留下来有效地选择新项目并搞砸了。有没有办法让它们与掉落的物品一起移动?信号indicesMoved似乎与我想要的完全一样,但它永远不会发射。我用错了吗?是否有不同/更好的方式?
答案 0 :(得分:2)
我认为您可能需要让模型告诉您哪些内容被删除,因为它最终将处理此移动:
class Model(QStandardItemModel):
rowDropped = pyqtSignal(int)
def dropMimeData(self, *args):
success = super(Model, self).dropMimeData(*args)
if success:
self.rowDropped.emit(args[2])
return success
这将从模型中发出丢弃发生的行号。您的视图已经知道哪个项目是从自己的事件中拖放的。
我确信在跟踪对象方面还有其他方法,然后在完成删除后再次查询它。