Correctly create QDir from QFileInfo
-
I loop through subfolders (only folders) and have these subfolders as QFileInfo.
How do I correctly and portably convert a QFileInfo which I know to be a folder into a QDir object?
The API of QFileInfo utterly confuses me on this:QDir QFileInfo::dir() const Returns the path of the object's parent directory as a QDir object. Note: The QDir returned always corresponds to the object's parent directory, even if the QFileInfo represents a directory.
So that's not the right one...unfortunately, they don't link to anything that might help.
How about path() ?QString QFileInfo::path() const Returns the file's path. This doesn't include the file name.
I don't have a "file", so I assume this isn't it, either.
QString QFileInfo::absoluteFilePath() const Returns an absolute path including the file name.
Again, includes a file name. No mention on how folders are treated.
QString QFileInfo::absolutePath() const Returns a file's path absolute path. This doesn't include the file name.
Again, no mention on how a folder would be treated. Would I get the folder itself, or the parent folder?
I assume that something like path() or absolutePath() probably works, but it leaves an uneasy feeling because the docs don't say that it will work.
Please, what have I overlooked?
-
@Asperamanca in general description, the mystery is resolved:
The file's type is obtained with isFile(), isDir() and isSymLink().
So, the last part of the file path can be a file or a dir. Thus:
How do I correctly and portably convert a QFileInfo which I know to be a folder into a QDir object?
QString getDirectoryPath() { const QFileInfo info(something...); if (info.isDir()) { return info.absoluteFilePath(); } if (info.isFile()) { return info.absolutePath(); } return {}; }
-
@Asperamanca , @sierdzio
Am I misunderstanding or doesn't QDir QFileInfo::absoluteDir() const (but not QDir QFileInfo::dir() const) do exactly what you ask for? -
@JonB said in Correctly create QDir from QFileInfo:
@Asperamanca , @sierdzio
Am I misunderstanding or doesn't QDir QFileInfo::absoluteDir() const (but not QDir QFileInfo::dir() const) do exactly what you ask for?That's the question. Just by reading the documentation, I couldn't say if that would yield the folder itself, or it's parent folder.
-
@sierdzio said in Correctly create QDir from QFileInfo:
@Asperamanca in general description, the mystery is resolved:
The file's type is obtained with isFile(), isDir() and isSymLink().
So, the last part of the file path can be a file or a dir. Thus:
How do I correctly and portably convert a QFileInfo which I know to be a folder into a QDir object?
QString getDirectoryPath() { const QFileInfo info(something...); if (info.isDir()) { return info.absoluteFilePath(); } if (info.isFile()) { return info.absolutePath(); } return {}; }
I get the isDir() part.
But isDir() does not link to absoluteFilePath(), absoluteFilePath() does not mention how it handles a folder, and so on.
The obvious counterpart to isDir() would be dir(), but that must have tripped up so many people that they explicitly mentioned that it will always return the parent folder, no matter what.You are likely right, but I can't see how you got the information from the documentation.
-
@Asperamanca said in Correctly create QDir from QFileInfo:
That's the question. Just by reading the documentation, I couldn't say if that would yield the folder itself, or it's parent folder.
But that's why I showed you the alternative which NOT to use, which says "parent directory". The one I recommend specifically does not say that, so it seems clear to me. I don't understand why you don't try it, in preference to the other way you are trying now....
-
@Asperamanca there's nothing wrong discussing documentation. As it is, you find the documentation confusing so you should check and maybe open an issue on the bug tracker to improve the documentation of the class. Wording might be improved and maybe some more code snippets could be useful.
Even better, provide a patch to help fix the situation :-)
-
@Asperamanca said in Correctly create QDir from QFileInfo:
@sierdzio said in Correctly create QDir from QFileInfo:
@Asperamanca in general description, the mystery is resolved:
The file's type is obtained with isFile(), isDir() and isSymLink().
So, the last part of the file path can be a file or a dir. Thus:
How do I correctly and portably convert a QFileInfo which I know to be a folder into a QDir object?
QString getDirectoryPath() { const QFileInfo info(something...); if (info.isDir()) { return info.absoluteFilePath(); } if (info.isFile()) { return info.absolutePath(); } return {}; }
I get the isDir() part.
But isDir() does not link to absoluteFilePath(), absoluteFilePath() does not mention how it handles a folder, and so on.You are right, this should be improved and explained in all relevant places.
You are likely right, but I can't see how you got the information from the documentation.
To me,
The file's type is obtained with
implies that for QFileInfo, the last section of a path is always treated as a file. It's up to the user to check if it really is a "file file" or "directory file".There are more clues to this, like:
QFileInfo provides information about a file's name and position (path) in the file system, its access rights and whether it is a directory or symbolic link, etc.
And it also makes sense in general: since
QDir::entryInfoList()
returns a list of files and directories, it is quite clear to me that QFileInfo can point to either a file or a directory. You could argue the class name is wrong, but what the alternative would be?QFileSystemObjectInfo
?QFileOrDirectoryInfo
?QINodeInfo
? Each alternative comes with some downsides, too. -
Took the liberty to create a bug report for this: https://bugreports.qt.io/browse/QTBUG-120688
-