September 13, 2011

davidvaz davidvaz
Lab Rat
1 posts

QListWidget Drag & Drop External File Drop and Internal Reordering

 

Hi,

I am trying to have a QListWidget that accepts both external file drop and InternalMove for reordering.
I came up with 2 solutions:

  1. one that shows the drop indicator in both cases but in the reordering allows to drop outside
  2. one that has the internalmove working correctly but on external drop there is no dropindicator.

First Solution

  1. class FileList(QListWidget):
  2.     def __init__(self, parent=None):
  3.         super(FileList, self).__init__(parent)
  4.         self.setDragEnabled(True)
  5.         self.setAcceptDrops(True)
  6.         self.setDropIndicatorShown(True)
  7.         self.setSelectionMode(QAbstractItemView.SingleSelection)
  8.         #to allow internal reordering
  9.         self.setDefaultDropAction(Qt.MoveAction)
  10.  
  11.     def dropMimeData(self, index, mimeData, action):
  12.         if mimeData.hasUrls():
  13.             for url in mimeData.urls():
  14.                 file = QFileInfo(url.toLocalFile())
  15.                 item = QListWidgetItem(file.filePath())
  16.                 self.insertItem(index, item)
  17.         return True
  18.  
  19.     def mimeTypes(self):
  20.         return ["text/uri-list"]

Second Solution

  1. class FileList(QListWidget):
  2.     def __init__(self, parent=None):
  3.         super(FileList, self).__init__(parent)
  4.         self.setAcceptDrops(True)
  5.         self.setDragEnabled(True)
  6.         self.setSelectionMode(QAbstractItemView.SingleSelection)
  7.         self.setDropIndicatorShown(True)
  8.         self.setDragDropMode(QAbstractItemView.InternalMove)
  9.  
  10.     def dragEnterEvent(self, event):
  11.         if event.mimeData().hasUrls():
  12.             event.acceptProposedAction()
  13.         else:
  14.             super(FileList,self).dragEnterEvent(event)
  15.  
  16.     def dragMoveEvent(self, event):
  17.         if event.mimeData().hasUrls():
  18.             event.acceptProposedAction()
  19.         else:
  20.             super(FileList, self).dragMoveEvent(event)
  21.  
  22.     def dropEvent(self, event):
  23.         if event.mimeData().hasUrls():
  24.             urls = event.mimeData().urls()
  25.             if urls:
  26.                 for url in urls:
  27.                     QListWidgetItem(url.toLocalFile(),self)
  28.             event.acceptProposedAction()
  29.         super(FileList, self).dropEvent(event)

How can I combine this two to have drop indicator on external drop (like in the first example with dropMimeData) and allow internalMove (like in the second example)?

0 replies

 
  ‹‹ Compiling apiextractor on Mac OS X failed (lib not found libxml2)      How to set the depth of QWidget??? ››

You must log in to post a reply. Not a member yet? Register here!