gpt4 book ai didi

python - PyQt MimeData 文件名

转载 作者:行者123 更新时间:2023-12-01 04:12:41 24 4
gpt4 key购买 nike

我本质上是在重复 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com