是否可以在Windows资源管理器/ OS X Finder中打开文件夹,然后选择/突出显示该文件夹中的一个文件,并以跨平台方式执行此操作?现在,我做了类似的事情
QDesktopServices::openUrl( QUrl::fromLocalFile( path ) );
Run Code Online (Sandbox Code Playgroud)
哪里path
是我要打开的文件夹的完整路径.显然,这只会打开文件夹,我将不得不手动追踪我需要的文件.当该文件夹中有数千个文件时,这有点问题.
如果我将它作为该文件夹中特定文件的路径,那么该文件是用该mime类型的默认应用程序打开的,这不是我需要的.相反,我需要相当于"在Finder中显示"或"在资源管理器中显示"的功能.
Ivo*_*Ivo 42
Qt Creator(源代码)具有此功能,复制它是微不足道的:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
另外,相关博客文章(使用简单的代码,我还没有尝试过,所以我无法评论),是这个.
编辑:
当pathIn在Windows上包含空格时,原始代码中存在错误.如果参数包含空格,QProcess :: startDetached将自动引用参数.但是,Windows资源管理器将无法识别包含在引号中的参数,而是将打开默认位置.在Windows命令行中自己尝试:
echo. > "C:\a file with space.txt"
:: The following works
C:\Windows\explorer.exe /select,C:\a file with space.txt
:: The following does not work
C:\Windows\explorer.exe "/select,C:\a file with space.txt"
Run Code Online (Sandbox Code Playgroud)
从而,
QProcess::startDetached(explorer, QStringList(param));
Run Code Online (Sandbox Code Playgroud)
改为
QString command = explorer + " " + param;
QProcess::startDetached(command);
Run Code Online (Sandbox Code Playgroud)
lia*_*iaK 10
可能你可以QFileDialog::getOpenFileName
用来获取文件名.该文档可在这里.以上函数将返回包括文件名和扩展名的完整路径 ,如果任何 ..
然后你可以给
QDesktopServices::openUrl(path);
以默认应用程序打开文件,其中path
将QString
返回QFileDialog::getOpenFileName
.
希望能帮助到你..
这是我根据之前答案的输入使用的代码。该版本不依赖于 Qt Creator 中的其他方法,接受文件或目录,并且具有错误处理和其他平台的回退模式:
void Util::showInFolder(const QString& path)
{
QFileInfo info(path);
#if defined(Q_OS_WIN)
QStringList args;
if (!info.isDir())
args << "/select,";
args << QDir::toNativeSeparators(path);
if (QProcess::startDetached("explorer", args))
return;
#elif defined(Q_OS_MAC)
QStringList args;
args << "-e";
args << "tell application \"Finder\"";
args << "-e";
args << "activate";
args << "-e";
args << "select POSIX file \"" + path + "\"";
args << "-e";
args << "end tell";
args << "-e";
args << "return";
if (!QProcess::execute("/usr/bin/osascript", args))
return;
#endif
QDesktopServices::openUrl(QUrl::fromLocalFile(info.isDir()? path : info.path()));
}
Run Code Online (Sandbox Code Playgroud)
在Windows资源管理器中打开文件(不是浏览器)
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);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
15621 次 |
最近记录: |