作者:小 琛
欢迎转载,请标明出处

bool copyFilePath(const QString& filePath) {
    bool ret = false;
    if (isFileExists(filePath)) {
        // create a clipboard and set the MIME type to a file path list
        QClipboard* clipboard = QApplication::clipboard();
        QMimeData* mimeData = new QMimeData();
        QList<QUrl> urls;
        urls.append(QUrl::fromLocalFile(filePath));
        mimeData->setUrls(urls);
        clipboard->setMimeData(mimeData);

        // do copy
        QProcess* process = new QProcess(this);
        process->start("cmd.exe", QStringList() << "/c"
                                                << "xcopy"
                                                << "/y"
                                                << "/q"
                                                << "\"" + filePath + "\""
                                                << "\"%temp%\"");
        // process->waitForFinished();//阻塞等待
        ret = true;
    }
    return ret;
}
07-11 18:58