[Qt-interest] QFileInfo doesn't work after QDir::setCurrent. Bug?
Scott Aron Bloom
Scott.Bloom at onshorecs.com
Thu Jun 10 20:55:33 CEST 2010
You may want to try playing with "setCaching"
You can also call refresh...
That said, From http://doc.trolltech.com/4.6/qfileinfo.html#details the
docs clearly say much of QFileInfo is cached for performance issues...
Performance Issues
Some of QFileInfo's functions query the file system, but for performance
reasons, some functions only operate on the file name itself. For
example: To return the absolute path of a relative file name,
absolutePath() has to query the file system. The path() function,
however, can work on the file name directly, and so it is faster.
Note: To speed up performance, QFileInfo caches information about the
file.
To speed up performance, QFileInfo caches information about the file.
Because files can be changed by other users or programs, or even by
other parts of the same program, there is a function that refreshes the
file information: refresh(). If you want to switch off a QFileInfo's
caching and force it to access the file system every time you request
information from it call setCaching(false).
See also QDir and QFile.
Scott
-----Original Message-----
From: Gabriel M. Beddingfield [mailto:gabrbedd at gmail.com]
Sent: Thursday, June 10, 2010 11:32 AM
To: Scott Aron Bloom
Cc: Nikos Chantziaras; qt-interest at trolltech.com
Subject: Re: [Qt-interest] QFileInfo doesn't work after
QDir::setCurrent. Bug?
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