This question already has answers here:
Cross-platform way of constructing an FS path with Qt [duplicate]

(3个答案)



How to “Reveal in Finder” or “Show in Explorer” with Qt

(5个答案)


2年前关闭。




我正在尝试运行命令以在explorer.exe中打开特定位置并选择特定文件:
QUrl url = QUrl::fromUserInput(file.absoluteFilePath());
QString str = "explorer.exe /select,\"" + url.toString() + "\"";
system(str.toStdString().c_str());

这适用于Windows位置。

但是,由于我也可以从NAS和MAC用户打开位置,因此可以在其中创建文件夹,所以我遇到了如下文件夹的问题:



由于在Mac上可能使用斜杠来命名文件/文件夹,因此system()功能无法识别斜线,并且QString将其转换为U+002F

在Windows资源管理器中,它显示为:



并且,如果我使用命令提示符导航到这样的文件夹,它将显示文件夹名称,如下所示:



有谁知道如何处理这种特殊情况或如何将此路径转换为可以作为explorer.exe的参数传递的路径?

/ *编辑08/31/2018 * /

我将代码更改为:
#include <Shlobj.h>
#include <atlstr.h>
void exportManager::BrowseToFile(QString filename)
{
TCHAR tchar[512];
USES_CONVERSION;
_tcscpy(tchar, A2T(filename.toStdString().c_str()));

ITEMIDLIST *pidl = ILCreateFromPath(tchar);
 if (pidl) {
    SHOpenFolderAndSelectItems(pidl, 0, 0, 0);
    ILFree(pidl);
 }
}
....
QString path = qFile.absoluteFilePath();
BrowseToFile(path.replace('/', '\\'));

但这仍然不能解决文件夹/文件名中的反斜杠和斜杠的问题。
似乎转换“filename.toStdString()。c_str()”会导致问题。

如果我打印(qDebug())的路径,则如下所示:
// NAS /文件夹\ uF022With \ uF022BackSLASH / file.ext

我找到了这个问题,这解决了我的问题。
QDesktopServices::openUrl with selecting specified file in explorer

最佳答案

您需要使用一个称为SHOpenFolderAndSelectItems()的函数来通过命令行调用explorer.exe
这是执行此操作的示例代码:

void BrowseToFile(LPCTSTR filename)
{
    ITEMIDLIST *pidl = ILCreateFromPath(filename);
    if(pidl) {
        SHOpenFolderAndSelectItems(pidl,0,0,0);
        ILFree(pidl);
    }
}

希望能有所帮助;干杯

10-07 19:36
查看更多