On Friday 04 April 2008 13:04:50 Bernhard Rosenkränzer wrote:
> I'm not 100% sure that this is a bug, but it should either be fixed or the
> documentation should be updated to mention this behavior:
>
> QTemporaryFile forgets the fileName() after it is close()d.

It's no accident. When you close the file, normally QTemporaryFile also 
deletes it (that's the default behaviour, but can be changed). At that point, 
you shouldn't be using the file anymore.

The code for QTemporaryFile::fileName() looks like this:

QString QTemporaryFile::fileName() const
{
    if(!isOpen())
        return QString();


The reason it does that is that it is no longer safe to assume the file name 
is valid for your purposes. You should keep the file open for exactly as long 
as you need it: don't close it too early, but don't leave it open if you 
don't need it anymore. You should only perform operations on the temporary 
file using the QTemporaryFile object.

Do not reopen the file using QFile and don't try to use the path that it 
created for you.

Technically speaking, you shouldn't even pass the file name to another 
process: on some systems (non-Windows), files can be deleted and replaced 
while processes have them open. The only way to safely use the same file is 
by sharing the same file descriptor and Qt does not offer a method to let you 
do that. I have a task open to research making QProcess accept opened QFiles 
as redirection targets.

In summary: don't use the filename. Use QTemporaryFile's I/O operations only.

-- 
Thiago José Macieira - thiago.macieira AT trolltech.com
Trolltech ASA - Sandakerveien 116, NO-0402 Oslo, Norway

Attachment: signature.asc
Description: This is a digitally signed message part.

Reply via email to