如何知道在qlistview中哪个项目被丢弃? [英] How to tell where an item was dropped in qlistview?
问题描述
我有一个自定义的QListView:
I have a custom 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
我真的需要知道项目的位置在拖放操作后被丢弃,所以其他的东西可以正确更新(我试图把拖放到一些从未设计过的东西,大的应用程序,而不是我的设计)。选择模型的当前索引和选择不遵循丢弃的项目,它们留在有效地选择一个新项目并拧紧东西。有没有办法使它们与丢弃的项目一起移动?信号索引已经看起来和我想要的一样,但它并没有被触发。我用错了吗?有没有不同的/更好的方式?
And I really need to know where the item was dropped after a drag and drop operation so other things can be properly updated (I'm trying to put drag and drop into something never designed for it, big app, not my design). The selection model's current index and selection don't follow the dropped item, they stay behind effectively selecting a new item and screwing things up. Is there a way to make them move with the dropped item? The signal indexesMoved seems exactly like what I want, but it never fires. Am I using it wrong? Is there a different/better way?
推荐答案
我想你可能需要真正让模型告诉你什么东西被丢弃,因为它将最终处理这个举措:
I think you might need to actually have the model tell you where something was dropped, since it will ultimately handle the move:
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
这将从模型,发生下降的行号。您的观点已经知道哪个项目是从其自己的事件中拖放的。
This will emit, from the model, the row number on which the drop occurred. Your view already knows which item was dragged and dropped from its own events.
我确定在跟踪对象方面还有其他方法,然后再次查询下降完成。
I am sure there are other ways in terms of tracking the object and then querying it again after the drop has completed.
这篇关于如何知道在qlistview中哪个项目被丢弃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!