pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

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

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

問題描述

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

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 是我要打開的文件夾的完整路徑.顯然,這只會打開文件夾,我必須手動追蹤我需要的文件.當該文件夾中有數千個文件時,這有點問題.

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.

如果我將其設置為該文件夾中特定文件的路徑,則該文件將使用該 MIME 類型的默認應用程序打開,這不是我所需要的.相反,我需要相當于在 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)這個功能,簡單復制一下:

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

另一篇相關的博文(代碼比較簡單,我沒試過所以無法評論),是這個.

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

Windows 上 pathIn 包含空格時,原始代碼中存在錯誤.QProcess::startDetached 會自動引用包含空格的參數.但是,Windows 資源管理器不會識別用引號括起來的參數,而是會打開默認位置.在 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);

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

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

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 厌氧反应器,IC厌氧反应器,厌氧三相分离器-山东创博环保科技有限公司 | 台湾Apex减速机_APEX行星减速机_台湾精锐减速机厂家代理【现货】-杭州摩森机电 | 明渠式紫外线杀菌器-紫外线消毒器厂家-定州市优威环保 | 美缝剂_美缝剂厂家_美缝剂加盟-地老板高端瓷砖美缝剂 | 美国查特CHART MVE液氮罐_查特杜瓦瓶_制造全球品质液氮罐 | 中式装修设计_室内中式装修_【云臻轩】中式设计机构 | 拉力机-拉力试验机-万能试验机-电子拉力机-拉伸试验机-剥离强度试验机-苏州皖仪实验仪器有限公司 | 土壤肥料养分速测仪_测土配方施肥仪_土壤养分检测仪-杭州鸣辉科技有限公司 | MES系统工业智能终端_生产管理看板/安灯/ESOP/静电监控_讯鹏科技 | 实验室pH计|电导率仪|溶解氧测定仪|离子浓度计|多参数水质分析仪|pH电极-上海般特仪器有限公司 | 申江储气罐厂家,储气罐批发价格,储气罐规格-上海申江压力容器有限公司(厂) | 桑茶-七彩贝壳桑叶茶 长寿茶| China plate rolling machine manufacturer,cone rolling machine-Saint Fighter | 优秀的临床医学知识库,临床知识库,医疗知识库,满足电子病历四级要求,免费试用 | 正压送风机-多叶送风口-板式排烟口-德州志诺通风设备 | 即用型透析袋,透析袋夹子,药敏纸片,L型涂布棒-上海桥星贸易有限公司 | 骨密度检测仪_骨密度分析仪_骨密度仪_动脉硬化检测仪专业生产厂家【品源医疗】 | 根系分析仪,大米外观品质检测仪,考种仪,藻类鉴定计数仪,叶面积仪,菌落计数仪,抑菌圈测量仪,抗生素效价测定仪,植物表型仪,冠层分析仪-杭州万深检测仪器网 | 轴流风机-鼓风机-离心风机-散热风扇-罩极电机,生产厂家-首肯电子 | 减速机_上海宜嘉减速机| LNG鹤管_内浮盘价格,上装鹤管,装车撬厂家-连云港赛威特机械 | 合肥风管加工厂-安徽螺旋/不锈钢风管-通风管道加工厂家-安徽风之范 | Safety light curtain|Belt Sway Switches|Pull Rope Switch|ultrasonic flaw detector-Shandong Zhuoxin Machinery Co., Ltd | 合肥展厅设计-安徽展台设计-合肥展览公司-安徽奥美展览工程有限公司 | 厚壁钢管-厚壁无缝钢管-小口径厚壁钢管-大口径厚壁钢管 - 聊城宽达钢管有限公司 | 无尘烘箱_洁净烤箱_真空无氧烤箱_半导体烤箱_电子防潮柜-深圳市怡和兴机电 | 自动检重秤-动态称重机-重量分选秤-苏州金钻称重设备系统开发有限公司 | 衬氟旋塞阀-卡套旋塞阀-中升阀门首页 | elisa试剂盒-PCR试剂盒「上海谷研实业有限公司」 | 顶空进样器-吹扫捕集仪-热脱附仪-二次热解吸仪-北京华盛谱信仪器 | YJLV22铝芯铠装电缆-MYPTJ矿用高压橡套电缆-天津市电缆总厂 | 海外仓系统|国际货代系统|退货换标系统|WMS仓储系统|海豚云 | 鹤壁创新仪器公司-全自动量热仪,定硫仪,煤炭测硫仪,灰熔点测定仪,快速自动测氢仪,工业分析仪,煤质化验仪器 | 地脚螺栓_材质_标准-永年县德联地脚螺栓厂家 | 长沙一级消防工程公司_智能化弱电_机电安装_亮化工程专业施工承包_湖南公共安全工程有限公司 | 粉丝机械,粉丝烘干机,粉丝生产线-招远市远东粉丝机械有限公司 | 热回收盐水机组-反应釜冷水机组-高低温冷水机组-北京蓝海神骏科技有限公司 | 嘉兴恒升声级计-湖南衡仪声级计-杭州爱华多功能声级计-上海邦沃仪器设备有限公司 | 泥沙分离_泥沙分离设备_泥砂分离机_洛阳隆中重工机械有限公司 | 钢格板|热镀锌钢格板|钢格栅板|钢格栅|格栅板-安平县昊泽丝网制品有限公司 | Maneurop/美优乐压缩机,活塞压缩机,型号规格,技术参数,尺寸图片,价格经销商 |