我对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
如何正确使用此方法来检测给定的路径是文件夹还是文件?
发布于 2014-08-03 04:35:50
小心反斜杠,它们必须被转义。将路径声明替换为:
QString path = "C:\\Some\\Path";或使用:
QString path = "C:/Some/Path";希望这能解决你的问题。
发布于 2014-08-03 04:41:26
像"/"和"\"这样的斜杠在Linux和Windows中是不同的。您可以使用静态方法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( "/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.
https://stackoverflow.com/questions/25099029
复制相似问题