Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQT - copy file to clipboard

Tags:

python

pyqt

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?

like image 350
Qiao Avatar asked May 19 '11 17:05

Qiao


2 Answers

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)
like image 120
Nikita Nemkin Avatar answered Oct 26 '22 18:10

Nikita Nemkin


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_())
like image 5
Artur Gaspar Avatar answered Oct 26 '22 19:10

Artur Gaspar