Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用Qt“在Finder中显示”或“在Explorer中显示”_C++_Qt_Qt4 - Fatal编程技术网

C++ 如何使用Qt“在Finder中显示”或“在Explorer中显示”

C++ 如何使用Qt“在Finder中显示”或“在Explorer中显示”,c++,qt,qt4,C++,Qt,Qt4,是否可以在Windows Explorer/OS X Finder中打开一个文件夹,然后选择/突出显示该文件夹中的一个文件,并以跨平台的方式进行操作?现在,我做了一些类似的事情 QDesktopServices::openUrl( QUrl::fromLocalFile( path ) ); 其中path是我要打开的文件夹的完整路径。显然,这只会打开文件夹,我必须手动查找所需的文件。当该文件夹中有数千个文件时,这有点问题 如果我将其设置为该文件夹中特定文件的路径,则该文件将使用该mime类型的

是否可以在Windows Explorer/OS X Finder中打开一个文件夹,然后选择/突出显示该文件夹中的一个文件,并以跨平台的方式进行操作?现在,我做了一些类似的事情

QDesktopServices::openUrl( QUrl::fromLocalFile( path ) );
其中path是我要打开的文件夹的完整路径。显然,这只会打开文件夹,我必须手动查找所需的文件。当该文件夹中有数千个文件时,这有点问题

如果我将其设置为该文件夹中特定文件的路径,则该文件将使用该mime类型的默认应用程序打开,而这不是我所需要的。相反,我需要相当于在Finder中显示或在Explorer中显示的功能。

您可能可以使用QFileDialog::getOpenFileName获取文件名。文件是可用的。。上述函数将返回完整路径,包括文件名及其扩展名(如果有)

然后你可以给

QDesktopServices::openUrlpath

在默认应用程序中打开文件,其中路径将是QFileDialog::getOpenFileName返回的QString

希望有帮助。

有这个功能,复制它很简单:

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);
    }
}
因此,

改为

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

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

}

下面是我根据上一个答案输入的代码。此版本不依赖于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()));
}

此解决方案适用于Windows和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
}


澄清:你的意思是当你点击Finder中的显示或资源管理器中的显示菜单项时?@Austin是的,我的意思正是类似Finder中的显示或资源管理器中的显示。谢谢你的回答,但这不是我需要的。我更新了问题,试图澄清这一点。我需要的是在Finder中显示或在Explorer中显示功能。QDesktopServices的openUrl方法实际上在Explorer中显示一个文件/目录。您是否在路径字符串上预先添加了文件://?@liaK:OP只希望文件显示在资源管理器中,而不是打开它。你的方法将尝试打开文件本身。只是为了澄清5年后。。。如果path是一个目录,openUrlpath将在默认的文件浏览器中打开该目录,这在一般意义上是不会显示的,这意味着打开父目录。如果path是一个文件,它将以默认应用程序打开。您可以检测路径是否为文件,然后删除仅保留目录的文件名,但它不会在文件资源管理器中选择文件;有;如果您使用的是相对路径,则可能必须将其与QFileabsoluteFilePath等一起使用。cc@StradivariI在你链接的博客文章中测试了代码。它至少在Mac OS X上可以工作。如果需要打开包含空格的路径,可以使用如下内容:QProcess::startDetachedQStringexplorer/select,\%1\.argQDir::TonativeParatorsPath;在我尝试将所有这些移植到PyQt之前,您能确认它在Linux、文件选择和所有方面都能工作吗?“我们不能在这里选择一个文件,因为没有一个文件浏览器真正支持它”这句话听起来不太有希望。@Michael Scheper:我把DanDennedy对PyQt5的回答移植到了这里,请参见。我还没有在Linux上测试过它,但是它看起来非常简单。发布GPL代码并建议复制粘贴?这是个陷阱!如果您使用的是Windows资源管理器附带的操作系统,那就太好了,但是我的大多数用户都不喜欢使用这些操作系统。☺有点Windows专用的黑客,但效果很好,谢谢!不是跨平台的。您没有提供start函数所需的第三个参数&而且这不是跨平台的解决方案。我喜欢这种方法,因为它很简单。我将其移植到python/PyQt5,看一看。至于MacOS:注意路径必须是绝对的。只需添加一条返回指令,就可以抑制osascript的输出消息。还有一件事:在MacOS上只执行open dirPath会更简单,但如果path实际上是一个文件,它不会突出显示该文件。
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()));
}
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
}