开发者

PyQT - copy file to clipboard

Is it possible to copy file to a clipboard?

As if it was pressed "ctrl+c". So that when I press "ctrl+v" in some folder, it will appear here.

http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qclipboard.html - cannot find anything about files.

file = 'C:\foo.file'
clipboard = QtGui.QApplication.clipboard()
????

Is it pos开发者_运维问答sible at all?


Clipboard data is modelled with QMimeData class, which can contain a list of URLs, including local filesystem URLs.

from PyQt4 import QtCore, QtGui

app = QtGui.QApplication([])

data = QtCore.QMimeData()
url = QtCore.QUrl.fromLocalFile('c:\\foo.file')
data.setUrls([url])

app.clipboard().setMimeData(data)


Create QUrls of the files, store them in a QMimeData and paste the QMimeData to the QClipboard. (Works for multiple files, tested on KDE 4, not sure if works on Windows.)

import sys

from PyQt4.QtCore import QMimeData, QUrl
from PyQt4.QtGui import QApplication

app = QApplication(sys.argv)

# Create the urls.
url1 = QUrl('file1')
url2 = QUrl('file2')

# Create the mime data with the urls.
mime_data = QMimeData()
mime_data.setUrls([url1, url2])

# Copy the mime data to the clipboard.
clipboard = QApplication.clipboard()
clipboard.setMimeData(mime_data)

# Run the main loop.
# The X11 clipboard needs the event loop running.
sys.exit(app.exec_())
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜