KTemporaryFile Class Reference
from PyKDE4.kdecore import *
Inherits: QTemporaryFile → QFile → QIODevice → QObject
Detailed Description
\class KTemporaryFile ktemporaryfile.h <KTemporaryFile>
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
Methods | |
__init__ (self, KComponentData componentData=KGlobal.mainComponent()) | |
setPrefix (self, QString prefix) | |
setSuffix (self, QString suffix) |
Method Documentation
__init__ | ( | self, | ||
KComponentData | componentData=KGlobal.mainComponent() | |||
) |
Construct a new KTemporaryFile. The file will be stored in the temporary directory configured in KDE. The default prefix is the value of the default KDE temporary directory, plus your application's instance name. The default suffix is ".tmp".
- Parameters:
-
componentData The KComponentData to use for the name of the file and to look up the directory.
setPrefix | ( | self, | ||
QString | prefix | |||
) |
Sets a prefix to use when creating the file.
This function sets a prefix to use when creating the file. The random part of the filename will come after this prefix. The prefix can also change or modify the target directory. If prefix is an absolute path it will override the default temporary directory. If prefix is a relative directory it will be relative to the default temporary location. To set a relative directory for the current working directory you should use QTemporaryFile.setFileTemplate() directly.
- Parameters:
-
prefix The prefix to use when creating the file. Remember to end the prefix with a '/' if you are designating a directory.
setSuffix | ( | self, | ||
QString | suffix | |||
) |
Sets a suffix to use when creating the file.
Sets a suffix to use when creating the file. The random part of the filename will come before this suffix.
- Parameters:
-
suffix The suffix to use when creating the file.