class KTemporaryFile


Table of contents
Modules
kdecore Classes
All Classes
Module kdecore
Namespace global
Class KTemporaryFile
Inherits QTemporaryFile

A QTemporaryFile that will save in the KDE temp directory.

This class derives from QTemporaryFile and makes sure that your temporary files go in the temporary directory defined by KDE. (This is retrieved by using KStandardDirs to locate the "tmp" resource.) In general, whenever you would use a QTemporaryFile() use a KTemporaryFile() instead.

By default the filename will start with your application's instance name, followed by six random characters and an extension of ".tmp". You can use setPrefix() and setSuffix() to change the beginning and ending of the random name, as well as change the directory if you wish (read the descriptions of these functions for more information). For complex specifications, you may be better off calling QTemporaryFile.setFileTemplate() directly.

For example, let's make a new temporary file:

KTemporaryFile temp;

This temporary file will currently be stored in the default KDE temporary directory and have an extension of ".tmp". Now, let's change the directory:

temp.setPrefix("/var/lib/foodata/");

Now the temporary file will be stored in "/var/lib/foodata" instead of the default KDE temporary directory, with an extension of ".tmp". It's important to remember the leading and trailing slashes to properly define the path! Next, let's change the suffix to a particular extension:

temp.setSuffix(".pdf");

Now the temporary file will be stored in "/var/lib/foodata" and have an extension of ".pdf" instead of ".tmp".

Once you are done determining the name of the file, call open() to create the file.

if ( !temp.open() ) {
// handle error...
}

If open() is unable to create the file it will return false. If the call to open() returns true you are ready to use your temporary file. If you don't want the file removed automatically when the KTemporaryFile object is destroyed, you need to call setAutoRemove(false), but make sure you have a good reason for leaving your temp files around.

See also QTemporaryFile

Author Jaison Lee



methods