C++ Qt QFileInfo.isDir()返回错误的结果
我对Qt类QFileInfo有问题,这里是一些代码示例C++ Qt QFileInfo.isDir()返回错误的结果,c++,qt4.8,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
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();
}
QString path=“C:\\Some\\path”;
QFileInfo路径FileInfo(路径);
if(pathFileInfo.isDir()){
qDebug()小心反斜杠,它们必须转义。将路径声明替换为:
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( "/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();
}
QString path=QDir::tonativeoperators(“/your/path/here”);
//您也可以使用path2而不是path,因为它们都是相同的
QString path2=QDir::tonativeperators(“/your\path/here”);
QFileInfo路径FileInfo(路径);
if(pathFileInfo.isDir()){
qDebug()如果将路径更改为'QString path=“C:\\Some\\path”;'该怎么办?在示例中,这只是一个输入错误。路径具有正确的转义,而不是硬编码