Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. QListWidget Drag & Drop External File Drop and Internal Reordering

QListWidget Drag & Drop External File Drop and Internal Reordering

Scheduled Pinned Locked Moved Language Bindings
1 Posts 1 Posters 4.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    davidvaz
    wrote on 13 Sept 2011, 09:27 last edited by
    #1

    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)?

    1 Reply Last reply
    0

    1/1

    13 Sept 2011, 09:27

    • Login

    • Login or register to search.
    1 out of 1
    • First post
      1/1
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved