Home · All Classes · Modules

QTemporaryFile Class Reference
[QtCore module]

The QTemporaryFile class is an I/O device that operates on temporary files. More...

Inherits QFile.

Methods

Static Methods


Detailed Description

The QTemporaryFile class is an I/O device that operates on temporary files.

QTemporaryFile is used to create unique temporary files safely. The file itself is created by calling open(). The name of the temporary file is guaranteed to be unique (i.e., you are guaranteed to not overwrite an existing file), and the file will subsequently be removed upon destruction of the QTemporaryFile object. This is an important technique that avoids data corruption for applications that store data in temporary files. The file name is either auto-generated, or created based on a template, which is passed to QTemporaryFile's constructor.

Example:

 {
     QTemporaryFile file;
     if (file.open()) {
         // file.fileName() returns the unique file name
     }

     // the QTemporaryFile destructor removes the temporary file
 }

Reopening a QTemporaryFile after calling close() is safe. For as long as the QTemporaryFile object itself is not destroyed, the unique temporary file will exist and be kept open internally by QTemporaryFile.

A temporary file will have some static part of the name and some part that is calculated to be unique. The default filename qt_temp will be placed into the temporary path as returned by QDir.tempPath().

See also QDir.tempPath() and QFile.


Method Documentation

QTemporaryFile.__init__ (self)

Constructs a QTemporaryFile in QDir.tempPath(), using the file template "qt_temp.XXXXXX". The file is stored in the system's temporary directory.

See also setFileTemplate() and QDir.tempPath().

QTemporaryFile.__init__ (self, QString templateName)

Constructs a QTemporaryFile with a template filename of templateName. Upon opening the temporary file this will be used to create a unique filename. If the templateName does not contain XXXXXX it will automatically be appended and used as the dynamic portion of the filename.

If templateName is a relative path, the path will be relative to the current working directory. You can use QDir.tempPath() to construct templateName if you want use the system's temporary directory.

See also open() and fileTemplate().

QTemporaryFile.__init__ (self, QObject parent)

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Constructs a QTemporaryFile (with the given parent) in QDir.tempPath(), using the file template "qt_temp.XXXXXX".

See also setFileTemplate().

QTemporaryFile.__init__ (self, QString templateName, QObject parent)

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Constructs a QTemporaryFile with a template filename of templateName and the specified parent. Upon opening the temporary file this will be used to create a unique filename. If the templateName does end in XXXXXX it will automatically be appended and used as the dynamic portion of the filename.

If templateName is a relative path, the path will be relative to the current working directory. You can use QDir.tempPath() to construct templateName if you want use the system's temporary directory.

See also open() and fileTemplate().

bool QTemporaryFile.autoRemove (self)

Returns true if the QTemporaryFile is in auto remove mode. Auto-remove mode will automatically delete the filename from disk upon destruction. This makes it very easy to create your QTemporaryFile object on the stack, fill it with data, read from it, and finally on function return it will automatically clean up after itself.

Auto-remove is on by default.

See also setAutoRemove() and remove().

QTemporaryFile QTemporaryFile.createLocalFile (QString fileName)

Creates and returns a local temporary file whose contents are a copy of the contents of the given file.

QTemporaryFile QTemporaryFile.createLocalFile (QFile file)

This is an overloaded member function, provided for convenience.

Works on the given fileName rather than an existing QFile object.

QAbstractFileEngine QTemporaryFile.fileEngine (self)

QString QTemporaryFile.fileName (self)

Returns the complete unique filename backing the QTemporaryFile object. This string is null before the QTemporaryFile is opened, afterwards it will contain the fileTemplate() plus additional characters to make it unique.

See also fileTemplate().

QString QTemporaryFile.fileTemplate (self)

Returns the set file template. The default file template will be called qt_temp and be placed in QDir.tempPath().

See also setFileTemplate().

bool QTemporaryFile.open (self)

A QTemporaryFile will always be opened in QIODevice.ReadWrite mode, this allows easy access to the data in the file. This function will return true upon success and will set the fileName() to the unique filename used.

See also fileName().

bool QTemporaryFile.open (self, QIODevice.OpenMode flags)

QTemporaryFile.setAutoRemove (self, bool b)

Sets the QTemporaryFile into auto-remove mode if b is true.

Auto-remove is on by default.

See also autoRemove() and remove().

QTemporaryFile.setFileTemplate (self, QString name)

Sets the static portion of the file name to name. If the file template ends in XXXXXX that will automatically be replaced with the unique part of the filename, otherwise a filename will be determined automatically based on the static portion specified.

If name contains a relative file path, the path will be relative to the current working directory. You can use QDir.tempPath() to construct name if you want use the system's temporary directory.

See also fileTemplate().


PyQt 4.3.1 for X11Copyright © Riverbank Computing Ltd and Trolltech AS 2007Qt 4.3.0