如何“在Finder中显示”或“在资源管理器中显示”与Qt [英] How to "Reveal in Finder" or "Show in Explorer" with Qt

查看:413
本文介绍了如何“在Finder中显示”或“在资源管理器中显示”与Qt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Windows资源管理器/ OS X Finder中打开文件夹,然后选择/突出显示该文件夹中的一个文件,并以跨平台方式执行?现在,我做了像

  QDesktopServices :: openUrl(QUrl :: fromLocalFile(path)); 

其中 path 是文件夹的完整路径我想打开。显然,这将打开文件夹,我必须跟踪我需要手动的文件。这是一个有点问题,当该文件夹中有成千上万的文件。



如果我把它作为该文件夹中的特定文件的路径,那么该文件是开放的与默认应用程序的mime类型,这不是我需要的。相反,我需要相当于在Finder中显示或在资源管理器中显示的功能。

解决方案

Qt Creator 具有此功能,可将其复制:

  void FolderNavigationWidget :: showInGraphicalShell(QWidget * parent,const QString& pathIn)
{
// Mac文件夹或文件。
#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在启动Windows资源管理器的路径。
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(告诉应用程序\Finder \以显示POSIX文件\%1 \)
.arg(pathIn);
QProcess :: execute(QLatin1String(/ usr / bin / osascript),scriptArgs);
scriptArgs.clear();
scriptArgs<< QLatin1String( - e)
<< QLatin1String(tell application \Finder\activate);
QProcess :: execute(/ usr / bin / osascript,scriptArgs);
#else
//我们不能在这里选择一个文件,因为没有文件浏览器真正支持它...
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
}

另一个相关的博客文章我没有尝试过,所以我不能评论),是。 p>

编辑:



当pathIn包含Windows上的空格时,原始代码中有一个错误。如果参数包含空格, QProcess :: startDetached 将自动引用参数。但是,Windows资源管理器无法识别包含在引号中的参数,而是将打开默认位置。在Windows命令行中自己尝试一下:

  echo。 > C:\ a file with space.txt
::以下工作
C:\Windows \explorer.exe / select,C:\a文件与space.txt
::以下不工作
C:\Windows\explorer.exe/ select,C:\a文件与space.txt

因此,

  QProcess :: startDetached(explorer, QStringList(param)); 

更改为

  QString command = explorer ++ param; 
QProcess :: startDetached(command);


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

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.

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 has this functionality, it's trivial to copy it:

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
}

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

Edit:

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:\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"

Thus,

QProcess::startDetached(explorer, QStringList(param));

is changed to

QString command = explorer + " " + param;
QProcess::startDetached(command);

这篇关于如何“在Finder中显示”或“在资源管理器中显示”与Qt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆