Qt QFileInfo.isDir()返回错误结果
Qt QFileInfo.isDir() returns wrong result
我有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"中存在dir路径时,pathFileInfo.isDir()返回false
如果我正确的路径并添加一个QDir::separator()到它pathFileInfo.isDir()返回true
如何正确使用此方法来检测是否给定的路径是文件夹或文件?
小心使用反斜杠,它们必须被转义。替换路径声明:
QString path = "C:\Some\Path";
或使用:
QString path = "C:/Some/Path";
像"/"和""这样的斜杠在Linux和Windows中是不同的。您可以使用static方法QString QDir::toNativeSeparators ( const QString & pathName )
来获得具有正确分隔符的正确路径。
那么就这样做:
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( "/yourpath/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();
}
这是文档的链接
相关文章:
- (C++)分析树以计算返回错误值的简单算术表达式
- glad 导致 glfwSwapBuffers 返回错误消息
- 我的模板类方法返回错误类型?
- 共享内存:MapViewOfFile 返回错误 5
- 使用 std::forward_list 返回错误的队列实现
- FFMPEG:sws_scale返回错误:切片参数 0, 2160 无效
- __int64 CString 返回错误的值 - C++ MFC
- C++ OpenGL 中,绘制形状返回错误
- Python os.system() 返回错误值
- 函数从指针 c++ 中获取错误的值并返回错误的答案
- LibClang clang_getArgType() 返回错误的类型
- 除法函数返回错误的值
- 当命令失败时,gzip 会在 C++ 中返回错误代码吗?
- 矩阵逆变器返回错误的反转
- openCL-创建子缓冲区返回错误代码13
- Qt SQL LIKE语句返回错误
- GetLastError()从FtpPutFile()返回错误6
- PfCreateInterface返回错误120(未实现)
- 为什么 Pa_GetStreamTime() 返回错误?
- G++ 在尝试使用 G++ 使用 openCV 进行编译后返回错误