首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Qt QFileInfo.isDir()返回错误的结果

Qt QFileInfo.isDir()返回错误的结果
EN

Stack Overflow用户
提问于 2014-08-03 04:18:05
回答 2查看 1.8K关注 0票数 0

我对Qt类QFileInfo有问题,下面是一些代码示例

代码语言:javascript
复制
 QString path = "C:\\Some\\Path";
 QFileInfo pathFileInfo(path);
 if (pathFileInfo.isDir()){
     qDebug() << "path is dir, cdUp";
     pathDir.cdUp();
  } else {
      qDebug() << "path is not dir, getting dir";
      pathDir = pathFileInfo.dir();
  }

当目录路径存在于文件夹"Some“中时,pathFileInfo.isDir()返回false

如果我更正了路径并添加了QDir::separator(),pathFileInfo.isDir()返回true

如何正确使用此方法来检测给定的路径是文件夹还是文件?

EN

回答 2

Stack Overflow用户

发布于 2014-08-03 04:35:50

小心反斜杠,它们必须被转义。将路径声明替换为:

代码语言:javascript
复制
QString path = "C:\\Some\\Path";

或使用:

代码语言:javascript
复制
QString path = "C:/Some/Path";

希望这能解决你的问题。

票数 1
EN

Stack Overflow用户

发布于 2014-08-03 04:41:26

"/""\"这样的斜杠在Linux和Windows中是不同的。您可以使用静态方法QString QDir::toNativeSeparators ( const QString & pathName )为您的平台获取带有正确分隔符的正确路径。

所以就这么做吧:

代码语言:javascript
复制
QString path = QDir::toNativeSeperators( "/your/path/here" );
//you can also use path2 instead of path since they are both the same
QString path2 = QDir::toNativeSeperators( "/your\path/here" );
 QFileInfo pathFileInfo(path);
 if (pathFileInfo.isDir()){
     qDebug() << "path is dir, cdUp";
     pathDir.cdUp();
  } else {
      qDebug() << "path is not dir, getting dir";
      pathDir = pathFileInfo.dir();
  }

Here is a link to the documentation.

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25099029

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档