1

I am essentially repeating a question that was asked (but not answered) in the comments of PyQt: Getting file name for file dropped in app .

What I'd like to be able to do, a la that post, is convert an output from a file drop event in pyqt that currently looks like this: /.file/id=6571367.661326 into an actual file path (i.e. /.Documents/etc./etc./myProject/fileNeeded.extension)

so that I can make use of the file that made the attempted QDropEvent. how to do this. Any thoughts?

EDIT: As mentioned below in the comments, this appears to be a platform specific problem. I am running Mac OS X El Capitan (10.11.2)

Community
  • 1
  • 1
Ethan Kay
  • 657
  • 6
  • 24
  • 1
    I think you should point out that the problem is platform specific. On my Ubuntu @ekhumoro's solution works just fine. – Alexander Lutsenko Jan 09 '16 at 16:10
  • 1
    [Similar question](http://stackoverflow.com/q/33148542/984421) (unanswered). Looks like this issue has been fixed in Qt-5.4.1, though. – ekhumoro Jan 09 '16 at 23:07

1 Answers1

2

I figured out the solution after translating Obj-C code found in https://bugreports.qt.io/browse/QTBUG-40449. Note that this solution is only necessary for Macs running OS X Yosemite or later AND not running PyQt5 (i.e. running v.4.8 in my case).

import objc
import CoreFoundation as CF

def getUrlFromLocalFileID(self, localFileID):
    localFileQString = QString(localFileID.toLocalFile())
    relCFStringRef = CF.CFStringCreateWithCString(
        CF.kCFAllocatorDefault,
        localFileQString.toUtf8(),
        CF.kCFStringEncodingUTF8
        )
    relCFURL = CF.CFURLCreateWithFileSystemPath(
        CF.kCFAllocatorDefault,
        relCFStringRef,
        CF.kCFURLPOSIXPathStyle,
        False  # is directory
        )
    absCFURL = CF.CFURLCreateFilePathURL(
        CF.kCFAllocatorDefault,
        relCFURL,
        objc.NULL
        )
    return QUrl(str(absCFURL[0])).toLocalFile()

To see this working in a drag and drop situation, see below:

import sys
import objc
import CoreFoundation as CF
from PyQt4.QtGui import *
from PyQt4.QtCore import *

class MyListWidget(QListWidget):
    def __init__(self, parent):
        super(MyListWidget, self).__init__(parent)
        self.setAcceptDrops(True)
        self.setDragDropMode(QAbstractItemView.InternalMove)

    def getUrlFromLocalFileID(self, localFileID):
        localFileQString = QString(localFileID.toLocalFile())
        relCFStringRef = CF.CFStringCreateWithCString(
            CF.kCFAllocatorDefault,
            localFileQString.toUtf8(),
            CF.kCFStringEncodingUTF8
            )
        relCFURL = CF.CFURLCreateWithFileSystemPath(
            CF.kCFAllocatorDefault,
            relCFStringRef,
            CF.kCFURLPOSIXPathStyle,
            False   # is directory
            )
        absCFURL = CF.CFURLCreateFilePathURL(
            CF.kCFAllocatorDefault,
            relCFURL,
            objc.NULL
            )
        return QUrl(str(absCFURL[0])).toLocalFile()

    def dragEnterEvent(self, event):
        if event.mimeData().hasUrls():
            event.acceptProposedAction()
        else:
            super(MyListWidget, self).dragEnterEvent(event)

    def dragMoveEvent(self, event):
        super(MyListWidget, self).dragMoveEvent(event)

    def dropEvent(self, event):
        if event.mimeData().hasUrls():
            event.setDropAction(Qt.CopyAction)
            event.accept()
            links = []
            for url in event.mimeData().urls():
                if QString(url.toLocalFile()).startsWith('/.file/id='):
                    url = self.getUrlFromLocalFileID(url)
                    links.append(url)
                else:
                    links.append(str(url.toLocalFile()))
            for link in links:
                self.addItem(link)
        else:
            super(MyListWidget,self).dropEvent(event)

class MyWindow(QWidget):
    def __init__(self):
        super(MyWindow,self).__init__()
        self.setGeometry(100,100,300,400)
        self.setWindowTitle("Filenames")

        self.list = MyListWidget(self)
        layout = QVBoxLayout(self)
        layout.addWidget(self.list)

        self.setLayout(layout)

if __name__ == '__main__':

    app = QApplication(sys.argv)
    app.setStyle("plastique")

    window = MyWindow()
    window.show()

    sys.exit(app.exec_())
Ethan Kay
  • 657
  • 6
  • 24