美文网首页
Qt打开系统文件浏览器并选中文件

Qt打开系统文件浏览器并选中文件

作者: e196efe3d7df | 来源:发表于2022-03-04 15:37 被阅读0次

怎么在qt中打开系统文件浏览器,并选中文件呢?搬自https://stackoverflow.com/questions/3490336/how-to-reveal-in-finder-or-show-in-explorer-with-qt

// windowds
void OpenFileInExplorer()
{
   QString path = "C:/exampleDir/example.txt";

   QStringList args;

   args << "/select," << QDir::toNativeSeparators(path);

   QProcess *process = new QProcess(this);
   process->start("explorer.exe", args); 

}
// mac
void showFileInFolder(const QString &path){
    #ifdef _WIN32    //Code for Windows
        QProcess::startDetached("explorer.exe", {"/select,", QDir::toNativeSeparators(path)});
    #elif defined(__APPLE__)    //Code for Mac
        QProcess::execute("/usr/bin/osascript", {"-e", "tell application \"Finder\" to reveal POSIX file \"" + path + "\""});
        QProcess::execute("/usr/bin/osascript", {"-e", "tell application \"Finder\" to activate"});
    #endif
}

以上只摘了部分代码,完整的代码,可自行查看!

相关文章

网友评论

      本文标题:Qt打开系统文件浏览器并选中文件

      本文链接:https://www.haomeiwen.com/subject/pnkmrrtx.html