2012-10-11 53 views
1

我有一個自定義而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 

而且我真的需要知道項被拖拽後下降,拖放操作等等其他的東西可以正確地更新(我試圖把拖放到從來沒有爲它設計的東西,大的應用程序,而不是我的設計)。選擇模型的當前索引和選擇不跟隨丟棄的項目,他們留下來有效地選擇一個新項目並且搞砸了。有沒有辦法使它們隨着丟棄的物品移動?信號指標移動看起來完全像我想要的,但它永遠不會觸發。我用錯了嗎?有不同的/更好的方法嗎?

回答

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 

這將發出,從模型,行號在其上發生了下降。您的視圖已經知道哪些項目從自己的事件中被拖放。

我相信在跟蹤對象方面還有其他方法,然後在放置完成後再查詢它。

+0

太棒了!非常感謝 :) –

相關問題