Reputation: 16091
QClipboard offers several ways to copy stuff into the clipboard. There are high level functions for standard desktop (text, pixmaps, etc), but I could not figure out how to implement the standard copy file operation. Google did not help.
Upvotes: 1
Views: 1985
Reputation: 16091
Okay I found the solution to my problem. The problem is that gnome (working on linux) does its own thing. The file(s) are not stored in the text/uri-list
format like N1ghtLight mentioned, but uses the special x-special/gnome-copied-files
format. The following code did it:
// Get clipboard
QClipboard *cb = QApplication::clipboard();
// Ownership of the new data is transferred to the clipboard.
QMimeData* newMimeData = new QMimeData();
// Copy old mimedata
const QMimeData* oldMimeData = cb->mimeData();
for ( const QString &f : oldMimeData->formats())
newMimeData->setData(f, oldMimeData->data(f));
// Copy path of file
newMimeData->setText(_file->absolutePath());
// Copy file
newMimeData->setUrls({QUrl::fromLocalFile(_file->absolutePath())});
// Copy file (gnome)
QByteArray gnomeFormat = QByteArray("copy\n").append(QUrl::fromLocalFile(_file->absolutePath()).toEncoded());
newMimeData->setData("x-special/gnome-copied-files", gnomeFormat);
// Set the mimedata
cb->setMimeData(newMimeData);
Upvotes: 2
Reputation: 2102
Just put appropriate mime type and URL of the local file into clipboard. Docs reference.
QMimeData* mimeData = new QMimeData();
mimeData->setData("text/uri-list", "file:///C:/fileToCopy.txt");
clipboard->setMimeData(mimeData);
You can use static method QUrl::fromLocalFile to get QUrl instance to be used in mimeData->setData
:
mimeData->setData("text/uri-list", QUrl::fromLocalFile("C:/fileToCopy.txt"));
Upvotes: 3