Qt 打开操作系统(OS)文件管理器

目录

Qt 之打开系统文件管理器

Qt 使用资源管理器打开文件夹,并定位到指定文件。

=====================

Qt 之打开系统文件管理器

打开文件管理器有两种方案:

1、使用 QProcess 调用win系统自带 explorer 程序打开

        //获取程序当前目录    
        QString path = "E:\\VS2015";
     
        QDir dir;
        QFileInfo fi(path);
     
        if ((fi.isDir() || dir.mkdir(path))) {
            //创建PC端对应文件夹
        }
     
        //将地址中的"/"替换为"\",因为在Windows下使用的是"\"。
        path.replace("/", "\\");
     
        //打开文件管理器路径
        QProcess::startDetached("explorer " + path);
     
        qDebug() << "onClickOpenFolder " << path;

2、使用 QDesktopServices 打开

        QDesktopServices::openUrl(QUrl("file:E:\\VS2015", QUrl::TolerantMode));
        QDesktopServices::openUrl(QUrl("file:E:\\2-SNwriter V1.20.20.0.0", QUrl::TolerantMode));
        QDesktopServices::openUrl(QUrl("file:X:\\Demo", QUrl::TolerantMode));

注:推荐带 file 的格式,不带 file 的格式(如下),无法打开含有空格和共享文件目录

    QDesktopServices::openUrl(QUrl("E:\\VS2015", QUrl::TolerantMode));
————————————————
版权声明:本文为CSDN博主「虫师魁拔」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/tq501501/article/details/112361071

官方手册:

enum QUrl::ParsingMode

The parsing mode controls the way QUrl parses strings.

Constant

Value

Description

QUrl::TolerantMode

0

QUrl will try to correct some common errors in URLs. This mode is useful for parsing URLs coming from sources not known to be strictly standards-conforming.

QUrl::StrictMode

1

Only valid URLs are accepted. This mode is useful for general URL validation.

QUrl::DecodedMode

2

QUrl will interpret the URL component in the fully-decoded form, where percent characters stand for themselves, not as the beginning of a percent-encoded sequence. This mode is only valid for the setters setting components of a URL; it is not permitted in the QUrl constructor, in fromEncoded() or in setUrl(). For more information on this mode, see the documentation for QUrl::FullyDecoded.

In TolerantMode, the parser has the following behaviour:

  • Spaces and "%20": unencoded space characters will be accepted and will be treated as equivalent to "%20".
  • Single "%" characters: Any occurrences of a percent character "%" not followed by exactly two hexadecimal characters (e.g., "13% coverage.html") will be replaced by "%25". Note that one lone "%" character will trigger the correction mode for all percent characters.
  • Reserved and unreserved characters: An encoded URL should only contain a few characters as literals; all other characters should be percent-encoded. In TolerantMode, these characters will be accepted if they are found in the URL: space / double-quote / "<" / ">" / "" / "^" / "`" / "{" / "|" / "}" Those same characters can be decoded again by passing QUrl::DecodeReserved to toString() or toEncoded(). In the getters of individual components, those characters are often returned in decoded form.

When in StrictMode, if a parsing error is found, isValid() will return false and errorString() will return a message describing the error. If more than one error is detected, it is undefined which error gets reported.

Note that TolerantMode is not usually enough for parsing user input, which often contains more errors and expectations than the parser can deal with. When dealing with data coming directly from the user -- as opposed to data coming from data-transfer sources, such as other programs -- it is recommended to use fromUserInput().

See also fromUserInput(), setUrl(), toString(), toEncoded(), and QUrl::FormattingOptions.

Qt 使用资源管理器打开文件夹,并定位到指定文件。

目录

    问题描述
    解决方案

问题描述

之前使用qt在资源管理器打开某路径,方法如下:

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

上述方法有弊端,比如要查看文件夹A下面的B文件,上述方法可以打开A文件夹,但是如果A文件夹中的内容比较多,B文件在下面,还需要自己查找B文件的位置。

解决方案

    const QString explorer = "explorer";
    QStringList param;
    if (!QFileInfo(path).isDir())
        param << QLatin1String("/select,");
    QProcess::startDetached(explorer, param);

————————————————
版权声明:本文为CSDN博主「脏不张」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/ji3009/article/details/117173039

你可能感兴趣的:(Qt,qt,开发语言)