Qt Creator tiene esta funcionalidad, es trivial para copiarlo:
void FolderNavigationWidget::showInGraphicalShell(QWidget *parent, const QString &pathIn)
{
// Mac, Windows support folder or file.
#if defined(Q_OS_WIN)
const QString explorer = Environment::systemEnvironment().searchInPath(QLatin1String("explorer.exe"));
if (explorer.isEmpty()) {
QMessageBox::warning(parent,
tr("Launching Windows Explorer failed"),
tr("Could not find explorer.exe in path to launch Windows Explorer."));
return;
}
QString param;
if (!QFileInfo(pathIn).isDir())
param = QLatin1String("/select,");
param += QDir::toNativeSeparators(pathIn);
QString command = explorer + " " + param;
QProcess::startDetached(command);
#elif defined(Q_OS_MAC)
Q_UNUSED(parent)
QStringList scriptArgs;
scriptArgs << QLatin1String("-e")
<< QString::fromLatin1("tell application \"Finder\" to reveal POSIX file \"%1\"")
.arg(pathIn);
QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs);
scriptArgs.clear();
scriptArgs << QLatin1String("-e")
<< QLatin1String("tell application \"Finder\" to activate");
QProcess::execute("/usr/bin/osascript", scriptArgs);
#else
// we cannot select a file here, because no file browser really supports it...
const QFileInfo fileInfo(pathIn);
const QString folder = fileInfo.absoluteFilePath();
const QString app = Utils::UnixUtils::fileBrowser(Core::ICore::instance()->settings());
QProcess browserProc;
const QString browserArgs = Utils::UnixUtils::substituteFileBrowserParameters(app, folder);
if (debug)
qDebug() << browserArgs;
bool success = browserProc.startDetached(browserArgs);
const QString error = QString::fromLocal8Bit(browserProc.readAllStandardError());
success = success && error.isEmpty();
if (!success)
showGraphicalShellError(parent, app, error);
#endif
}
Otro, entrada de blog relacionada (con el código más simple, no he probado de modo que pueda 't comment), es this.
Editar:
Hay un error en el código original cuando Pathin contiene espacios en Windows. QProcess::startDetached citará automáticamente un parámetro si contiene espacios. Sin embargo, Windows Explorer no reconocerá un parámetro envuelto entre comillas, y en su lugar abrirá la ubicación predeterminada. Inténtelo usted mismo en la línea de comandos de 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"
Por lo tanto,
QProcess::startDetached(explorer, QStringList(param));
se cambia a
QString command = explorer + " " + param;
QProcess::startDetached(command);
Para aclarar: ¿Quieres decir como cuando se hace clic en un "Reveal en Buscador "o" Mostrar en el Explorador "elemento de menú? –
@Austin Sí, lo que quise decir es exactamente algo así como "Revelar en Finder" o "Mostrar en Explorer" – nnc