久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何“在 Finder 中顯示"或“在資源管理器中顯

How to quot;Reveal in Finderquot; or quot;Show in Explorerquot; with Qt(如何“在 Finder 中顯示或“在資源管理器中顯示與 Qt)
本文介紹了如何“在 Finder 中顯示"或“在資源管理器中顯示"與 Qt的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

是否可以在 Windows 資源管理器/OS X Finder 中打開一個(gè)文件夾,然后選擇/突出顯示該文件夾中的一個(gè)文件,并以跨平臺(tái)方式執(zhí)行此操作?現(xiàn)在,我做類似的事情

Is it possible to open a folder in Windows Explorer/OS X Finder and then select/highlight one file in that folder, and do it in a cross platform way? Right now, I do something like

QDesktopServices::openUrl( QUrl::fromLocalFile( path ) );

其中 path 是我要打開的文件夾的完整路徑.顯然,這只會(huì)打開文件夾,我必須手動(dòng)追蹤我需要的文件.當(dāng)該文件夾中有數(shù)千個(gè)文件時(shí),這有點(diǎn)問題.

where path is a full path to folder I want to open. Obviously, this will just open the folder, and I'll have to track down the file I need manually. This is a bit of a problem when there are thousands of files in that folder.

如果我將其設(shè)置為該文件夾中特定文件的路徑,則該文件將使用該 MIME 類型的默認(rèn)應(yīng)用程序打開,這不是我所需要的.相反,我需要相當(dāng)于在 Finder 中顯示"或在資源管理器中顯示"的功能.

If I make it a path to specific file in that folder, then that file is open with default application for that mime type, and that is not what I need. Instead, I need the functionality equivalent to "Reveal in Finder" or "Show in Explorer".

推薦答案

Qt Creator (source)這個(gè)功能,簡(jiǎn)單復(fù)制一下:

Qt Creator (source) has this functionality, it's trivial to copy it:

void FileUtils::showInGraphicalShell(QWidget *parent, const QString &pathIn)
{
    const QFileInfo fileInfo(pathIn);
    // Mac, Windows support folder or file.
    if (HostOsInfo::isWindowsHost()) {
        const FileName explorer = Environment::systemEnvironment().searchInPath(QLatin1String("explorer.exe"));
        if (explorer.isEmpty()) {
            QMessageBox::warning(parent,
                                 QApplication::translate("Core::Internal",
                                                         "Launching Windows Explorer Failed"),
                                 QApplication::translate("Core::Internal",
                                                         "Could not find explorer.exe in path to launch Windows Explorer."));
            return;
        }
        QStringList param;
        if (!fileInfo.isDir())
            param += QLatin1String("/select,");
        param += QDir::toNativeSeparators(fileInfo.canonicalFilePath());
        QProcess::startDetached(explorer.toString(), param);
    } else if (HostOsInfo::isMacHost()) {
        QStringList scriptArgs;
        scriptArgs << QLatin1String("-e")
                   << QString::fromLatin1("tell application "Finder" to reveal POSIX file "%1"")
                                         .arg(fileInfo.canonicalFilePath());
        QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs);
        scriptArgs.clear();
        scriptArgs << QLatin1String("-e")
                   << QLatin1String("tell application "Finder" to activate");
        QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs);
    } else {
        // we cannot select a file here, because no file browser really supports it...
        const QString folder = fileInfo.isDir() ? fileInfo.absoluteFilePath() : fileInfo.filePath();
        const QString app = UnixUtils::fileBrowser(ICore::settings());
        QProcess browserProc;
        const QString browserArgs = UnixUtils::substituteFileBrowserParameters(app, folder);
        bool success = browserProc.startDetached(browserArgs);
        const QString error = QString::fromLocal8Bit(browserProc.readAllStandardError());
        success = success && error.isEmpty();
        if (!success)
            showGraphicalShellError(parent, app, error);
    }
}

另一篇相關(guān)的博文(代碼比較簡(jiǎn)單,我沒試過所以無法評(píng)論),是這個(gè).

Another, related blog post (with simpler code, I haven't tried it so I can't comment), is this.

Windows 上 pathIn 包含空格時(shí),原始代碼中存在錯(cuò)誤.QProcess::startDetached 會(huì)自動(dòng)引用包含空格的參數(shù).但是,Windows 資源管理器不會(huì)識(shí)別用引號(hào)括起來的參數(shù),而是會(huì)打開默認(rèn)位置.在 Windows 命令行中自行嘗試:

There is a bug in the original code when pathIn contains spaces on Windows. QProcess::startDetached will automatically quote a parameter if it contains spaces. However, Windows Explorer will not recognize a parameter wrapped in quotes, and will open the default location instead. Try it yourself in the Windows command line:

echo. > "C:a file with space.txt"
:: The following works
C:Windowsexplorer.exe /select,C:a file with space.txt  
:: The following does not work
C:Windowsexplorer.exe "/select,C:a file with space.txt"

因此,

QProcess::startDetached(explorer, QStringList(param));

改為

QString command = explorer + " " + param;
QProcess::startDetached(command);

這篇關(guān)于如何“在 Finder 中顯示"或“在資源管理器中顯示"與 Qt的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 国产一级二级视频 | 蜜臀久久99精品久久久久宅男 | 一级黄色录像片 | 天美传媒在线观看 | 欧美激情亚洲 | 岛国在线视频 | 欧美日韩a | 四虎黄色影院 | 毛片在线观看网站 | 久草福利在线视频 | 亚洲国产天堂 | 亚洲精品一二三区 | 一级免费黄色片 | 福利在线看 | 综合av网| 97精品在线观看 | 国产精品三级在线 | 日本香蕉视频 | 国产美女精品 | 欧美专区在线 | 色伊伊| 99伊人| 欧美在线免费观看 | 国产精品福利在线 | 美女黄色在线观看 | 成人国产精品视频 | 免费一区二区视频 | 日韩精品区 | 日韩女优在线观看 | 一区二区三区久久 | 欧美黄色一级 | 日本欧美亚洲 | 欧美激情视频在线 | 欧美日韩久久 | 久久福利网 | 欧美a在线观看 | 91成人国产| 天天色天天爱 | 在线黄网 | 在线a视频| 国产一区二区三区久久 |