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:

    one that shows the drop indicator in both cases but in the reordering allows to drop outside

    one that has the internalmove working correctly but on external drop there is no dropindicator.

    First Solution
    @
    class FileList(QListWidget):
    def init(self, parent=None):
    super(FileList, self).init(parent)
    self.setDragEnabled(True)
    self.setAcceptDrops(True)
    self.setDropIndicatorShown(True)
    self.setSelectionMode(QAbstractItemView.SingleSelection)
    #to allow internal reordering
    self.setDefaultDropAction(Qt.MoveAction)

    def dropMimeData(self, index, mimeData, action):
        if mimeData.hasUrls():
            for url in mimeData.urls():
                file = QFileInfo(url.toLocalFile())
                item = QListWidgetItem(file.filePath())
                self.insertItem(index, item)
        return True
    
    def mimeTypes(self):
        return ["text/uri-list"]
    

    @

    Second Solution
    @
    class FileList(QListWidget):
    def init(self, parent=None):
    super(FileList, self).init(parent)
    self.setAcceptDrops(True)
    self.setDragEnabled(True)
    self.setSelectionMode(QAbstractItemView.SingleSelection)
    self.setDropIndicatorShown(True)
    self.setDragDropMode(QAbstractItemView.InternalMove)

    def dragEnterEvent(self, event):
        if event.mimeData().hasUrls():
            event.acceptProposedAction()
        else:
            super(FileList,self).dragEnterEvent(event)
    
    def dragMoveEvent(self, event):
        if event.mimeData().hasUrls():
            event.acceptProposedAction()
        else:
            super(FileList, self).dragMoveEvent(event)
    
    def dropEvent(self, event):
        if event.mimeData().hasUrls():
            urls = event.mimeData().urls()
            if urls:
                for url in urls:
                    QListWidgetItem(url.toLocalFile(),self)
            event.acceptProposedAction()
        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)?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.