title: PySide: Drag and drop files into QListWidget
body: <p>This is my first entry here so apologies for any newbie mistakes. I've searched both here and Google with no luck before posting.</p>
  
  <p>I want to be able to add images to a QListWidget, using drag and drop from a file browser. Dropping a valid file on the list widget also needs to trigger a function in the main class of my app, and pass it the image path. </p>
  
  <p>I found <a href="https://stackoverflow.com/questions/4151637/pyqt4-drag-and-drop-files-into-qlistwidget">this code</a> that does exactly that, but for PyQt4. Importing QtCore and QtGui from PySide instead of PyQt4 produces a segmentation fault when triggering a drag and drop event. There are no error messages. </p>
  
  <p>I think I've traced this to an old way of handling signals and have tried using the new, more pythonic way described <a href="https://qt-project.org/wiki/Signals_and_Slots_in_PySide" rel="nofollow noreferrer">here</a>. I can't seem to get it working, while still passing a list of URLs between classes however. </p>
  
  <p>Any help would be greatly appreciated!</p>
  
  <pre><code>import sys
  import os
  from PyQt4 import QtGui, QtCore
  
  class TestListView(QtGui.QListWidget):
      def __init__(self, type, parent=None):
          super(TestListView, self).__init__(parent)
          self.setAcceptDrops(True)
          self.setIconSize(QtCore.QSize(72, 72))
  
      def dragEnterEvent(self, event):
          if event.mimeData().hasUrls:
              event.accept()
          else:
              event.ignore()
  
      def dragMoveEvent(self, event):
          if event.mimeData().hasUrls:
              event.setDropAction(QtCore.Qt.CopyAction)
              event.accept()
          else:
              event.ignore()
  
      def dropEvent(self, event):
          if event.mimeData().hasUrls:
              event.setDropAction(QtCore.Qt.CopyAction)
              event.accept()
              links = []
              for url in event.mimeData().urls():
                  links.append(str(url.toLocalFile()))
              self.emit(QtCore.SIGNAL("dropped"), links)
          else:
              event.ignore()
  
  class MainForm(QtGui.QMainWindow):
      def __init__(self, parent=None):
          super(MainForm, self).__init__(parent)
  
          self.view = TestListView(self)
          self.connect(self.view, QtCore.SIGNAL("dropped"), self.pictureDropped)
          self.setCentralWidget(self.view)
  
      def pictureDropped(self, l):
          for url in l:
              if os.path.exists(url):
                  print(url)                
                  icon = QtGui.QIcon(url)
                  pixmap = icon.pixmap(72, 72)                
                  icon = QtGui.QIcon(pixmap)
                  item = QtGui.QListWidgetItem(url, self.view)
                  item.setIcon(icon)        
                  item.setStatusTip(url)        
  
  def main():
      app = QtGui.QApplication(sys.argv)
      form = MainForm()
      form.show()
      app.exec_()
  
  if __name__ == '__main__':
      main()
  </code></pre>