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?
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_())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With