[Qt-interest] QFileInfo doesn't work after QDir::setCurrent. Bug?

Gabriel M. Beddingfield gabrbedd at gmail.com
Thu Jun 10 20:31:59 CEST 2010


Hi Scott,

On Thu, 10 Jun 2010, Scott Aron Bloom wrote:

> That's because your QFileInfo is based on a relative path.

Not exactly...  I think Nikos might be right.

> Now consider this:
>
>   QFileInfo finfo("test.txt");
>   QDir::setCurrent("/home");
>   qDebug() << "exists:" << finfo.exists();
>
> This prints "false".  finfo is not functional anymore and can't be used
> to get any valid information about "test.txt".

It would appear the QFileInfo waited for the exists() method 
to be called before assessing the absolute path.  This is 
not expected.  You would expect that finfo's absolute path 
would be resolved in the constructor.

I'll bet if he does QDir::setCurrent("/wherever/test/actually/is/")
that exists() will still return false.

> And to top it off:
>
>   QFileInfo finfo("test.txt");
>   qDebug() << "exists:" << finfo.exists();
>   QDir::setCurrent("/home");
>   qDebug() << "exists:" << finfo.exists();
>
> This prints "true" 2 times, and finfo works and can be used normally.
> Seems that calling any method on finfo fixes this.

Here, by calling the exists() method, Nikos is forcing the 
evaluation of the absolute path... which persists regardless 
of the current directory.

> Have I found a bug in Qt, or is this normal?

That's up to the Troll Gods... but I would consider this 
unexpected behavior.  Seems it should either be documented 
or "fixed."

They /do/ document that /some/ information is cached, and 
other info is queried... but they make few promises about 
when and what.

-gabriel



More information about the Qt-interest-old mailing list