Reputation: 17049
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 possible at all?
Upvotes: 9
Views: 8003
Reputation: 4552
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_())
Upvotes: 5
Reputation: 2820
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)
Upvotes: 9