c++ - Qt QFileInfo.isDir() 返回错误结果

标签 c++ qt4.8

我对 Qt 类 QFileInfo 有疑问,这里是一些代码示例

 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
如何正确使用此方法来检测给定路径是文件夹还是文件?

最佳答案

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

QString path = "C:\\Some\\Path";

或使用:

QString path = "C:/Some/Path";

希望这能解决您的问题。

关于c++ - Qt QFileInfo.isDir() 返回错误结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25099029/

相关文章:

c++ - qt 是否有来自 STL 的 find_if 之类的东西?

qt - 动态 QImage ,当没有指定初始大小时

c++ - 将文件从应用程序外部拖放到 QTableView 中

c++ - 像油漆一样添加颜色(颜色)(蓝色 + 黄色 = 绿色等)

linux - 删除 QDialogs 默认关闭按钮

c++ - Nginx - Cygwin 中的 nchan 模块

c++ - glsl 蒙皮障碍,谁能跳过那个?

c++ - QT4.8.5在QTextEdit上显示光标

c++ - 有没有办法使用 boost::program_options::parse_config_file 在 INI 文件中包含多个 "name=value"行?

c++ - C++ 中多线程的 join() 和 detach() 有什么不同?