如何將本地文件復制到QClipboard(在Gnome中)?

[英]How to copy local file to QClipboard (in Gnome)?


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.

QClipboard提供了幾種將內容復制到剪貼板中的方法。標准桌面有高級的功能(文本、像素地圖等),但是我不知道如何實現標准的拷貝文件操作。谷歌沒有幫助。

2 个解决方案

#1


3  

Just put appropriate mime type and URL of the local file into clipboard. Docs reference.

只需將本地文件的mime類型和URL放入剪貼板。文檔參考。

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 :

可以使用靜態方法QUrl::fromLocalFile獲得mimeData—>setData中使用的QUrl實例:

mimeData->setData("text/uri-list", QUrl::fromLocalFile("C:/fileToCopy.txt"));

#2


2  

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:

我找到了解決問題的方法。問題是gnome(在linux上工作)做自己的事情。文件不是像前面提到的N1ghtLight那樣存儲文本/uri列表格式,而是使用特殊的x-special/gnome- coped文件格式。下面的代碼做到了:

//  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);

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/09/16/720c40cafa83f0c6efa1d84255d44a8b.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com