复制图片的内容到剪贴板:
QImage img(path); Q_ASSERT(!img.isNull()); cb->setImage(img, QClipboard::Clipboard);
复制大量文件的路径:
void copyFilesToClipboard(const QStringList &paths)
{
// 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 file (gnome)
QByteArray gnomeFormat = QByteArray("copy\n");
QString text;
QList<QUrl> dataUrls;
for (QString path : paths) {
if (!path.isEmpty())
text += path + "\n";
dataUrls << QUrl(QFileInfo(path).absoluteFilePath());
gnomeFormat.append(QUrl::fromLocalFile(path).toEncoded()).append("\n");
}
//setText need to be called, or you can't clip the context to clipboard.
newMimeData->setText(text);
newMimeData->setUrls(dataUrls);
gnomeFormat.remove(gnomeFormat.length() - 1, 1);
newMimeData->setData("x-special/gnome-copied-files", gnomeFormat);
// Set the mimedata
cb->setMimeData(newMimeData);
}