python - 如何判断 qlistview 中的项目被放置在何处?

标签 python drag-and-drop pyqt pyqt4 qlistview

我有一个自定义的 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

而且我真的需要知道拖放操作后该项目被放置在哪里,以便可以正确更新其他内容(我试图将拖放放入从未为其设计的东西中,大应用程序,而不是我的设计)。选择模型的当前索引和选择不会跟随丢弃的项目,它们会有效地选择新项目并把事情搞砸。有没有办法让他们随着掉落的元素一起移动?信号indexMoved 看起来与我想要的一模一样,但它永远不会触发。难道是我用错了?有不同/更好的方法吗?

最佳答案

我认为你可能需要让模型告诉你东西被扔到了哪里,因为它最终会处理移动:

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

这将从模型中发出发生放置的行号。您的 View 已经知道哪个项目从其自己的事件中被拖放。

我确信还有其他方法可以跟踪对象,然后在放置完成后再次查询它。

关于python - 如何判断 qlistview 中的项目被放置在何处?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12846509/

相关文章:

actionscript-3 - 防止 Flex 树掉落反馈

C# ListView DragDrop 事件方法每次拖放执行两次

python - 使用日期进行计算,无需 import dateutil

python - Python C API 的常量正确性

python - 如何使用 python 从两个 csv 文件中获取值并将其放入单个 csv 文件中?

python - PyQt关闭子窗口问题,无限循环?

python - 如何在 PyQt 中的 QGraphicsView 旁边添加菜单栏(QMainWindow)?

python - 发布 django 时过滤 csrfmiddlewaretoken

3d - 将一个网格拖到另一个网格上并将其限制在侧面 Three.js 内

python - 在 PyQT4 中使用循环创建按钮网格