python - PyQt MimeData 文件名

标签 python drag-and-drop pyqt mime-types

我本质上是在重复 PyQt: Getting file name for file dropped in app 的评论中提出(但没有回答)的问题。 。

我希望能够做的就是转换 pyqt 中文件删除事件的输出,当前如下所示: /.file/id=6571367.661326实际文件路径(即/.Documents/etc./etc./myProject/fileNeeded.extension)

这样我就可以利用尝试进行 QDropEvent 的文件。这个怎么做。有什么想法吗?

编辑: 正如下面的评论中提到的,这似乎是一个特定于平台的问题。我运行的是 Mac OS X El Capitan (10.11.2)

最佳答案

我在翻译 https://bugreports.qt.io/browse/QTBUG-40449 中找到的 Obj-C 代码后找到了解决方案。请注意,此解决方案仅适用于运行 OS X Yosemite 或更高版本且不运行 PyQt5 的 Mac(即在我的情况下运行 v.4.8)。

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

要查看其在拖放情况下的工作情况,请参见下文:

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_())

关于python - PyQt MimeData 文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34689562/

相关文章:

python - Django:以 DRY 方式按模型上定义的属性进行过滤?

jQuery UI 可删除 : how to make it live?

reactjs - 如何解决 'react-dnd-html5-backend' 不包含默认导出?

python - 为列表中的每个名称创建选项卡

python - Python 中的类型转换

python - 从分组计数创建列(涉及日期时间列)

python - 更改 GUI 库 : QT, wxPython...还有什么?

python - QToolBar 的大小和与隐藏操作的对齐

python - PKCS11 总是会以相同的顺序找到对象吗?

javascript - 如何防止 React js 和 React-beautiful-dnd 中的原始容器掉落?