QFileInfo and QDir

You can pass a file system path to the construction function of QFileInfo like:


The string passed as the parameter of the constructor is not necessarily a real file or directory, just has the form of a file system path. From the QFileInfo, we can get a QDir object:

QDir dir=QFileInfo("C:\\a\\b/c/d/e").dir();


We can get the path of the QDir object using:



In the above example, the dir path is C:/a/b/c/d, regardless of whether e is a file or directory. The constructor of QFileInfo can also take a relative path as its parameter such as


Now there are some issues needed notice:

  • what is QFileInfo(“a”).dir().path()? It is ., which you may know is the current directory. But what is QFileInfo(“.”).dir().path()? It’s still ., not the parent folder of current directory.
  • What is QFileInfo(“C:/a”).dir().path()? It is C:/. Then what is QFileInfo(“C:/”).dir().path()? It is still C:/.
Posted in

Comments are closed, but trackbacks and pingbacks are open.