<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <html><head><title>QTemporaryFile Class Reference</title><style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm } a:link { color: #004faf; text-decoration: none } a:visited { color: #672967; text-decoration: none } td.postheader { font-family: sans-serif } tr.address { font-family: sans-serif } body { background: #ffffff; color: black; } </style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr /><td align="left" valign="top" width="32"><img align="left" border="0" height="32" src="images/rb-logo.png" width="32" /></td><td width="1">  </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="modules.html"><font color="#004faf">Modules</font></a></td></table><h1 align="center">QTemporaryFile Class Reference<br /><sup><sup>[<a href="qtcore.html">QtCore</a> module]</sup></sup></h1><p>The QTemporaryFile class is an I/O device that operates on temporary files. <a href="#details">More...</a></p> <p>Inherits <a href="qfile.html">QFile</a>.</p><h3>Methods</h3><ul><li><div class="fn" /><b><a href="qtemporaryfile.html#QTemporaryFile">__init__</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qtemporaryfile.html#QTemporaryFile-2">__init__</a></b> (<i>self</i>, QString <i>templateName</i>)</li><li><div class="fn" /><b><a href="qtemporaryfile.html#QTemporaryFile-3">__init__</a></b> (<i>self</i>, QObject <i>parent</i>)</li><li><div class="fn" /><b><a href="qtemporaryfile.html#QTemporaryFile-4">__init__</a></b> (<i>self</i>, QString <i>templateName</i>, QObject <i>parent</i>)</li><li><div class="fn" />bool <b><a href="qtemporaryfile.html#autoRemove">autoRemove</a></b> (<i>self</i>)</li><li><div class="fn" />QAbstractFileEngine <b><a href="qtemporaryfile.html#fileEngine">fileEngine</a></b> (<i>self</i>)</li><li><div class="fn" />QString <b><a href="qtemporaryfile.html#fileName">fileName</a></b> (<i>self</i>)</li><li><div class="fn" />QString <b><a href="qtemporaryfile.html#fileTemplate">fileTemplate</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qtemporaryfile.html#open">open</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qtemporaryfile.html#open-2">open</a></b> (<i>self</i>, QIODevice.OpenMode <i>flags</i>)</li><li><div class="fn" /><b><a href="qtemporaryfile.html#setAutoRemove">setAutoRemove</a></b> (<i>self</i>, bool <i>b</i>)</li><li><div class="fn" /><b><a href="qtemporaryfile.html#setFileTemplate">setFileTemplate</a></b> (<i>self</i>, QString <i>name</i>)</li></ul><h3>Static Methods</h3><ul><li><div class="fn" />QTemporaryFile <b><a href="qtemporaryfile.html#createLocalFile">createLocalFile</a></b> (QString <i>fileName</i>)</li><li><div class="fn" />QTemporaryFile <b><a href="qtemporaryfile.html#createLocalFile-2">createLocalFile</a></b> (QFile <i>file</i>)</li></ul><a name="details" /><hr /><h2>Detailed Description</h2><p>The QTemporaryFile class is an I/O device that operates on temporary files.</p> <p>QTemporaryFile is used to create unique temporary files safely. The file itself is created by calling <a href="qtemporaryfile.html#open">open</a>(). 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.</p> <p>Example:</p> <pre class="cpp"> <span class="comment">// Within a function/method...</span> <span class="type">QTemporaryFile</span> file; <span class="keyword">if</span> (file<span class="operator">.</span>open()) { <span class="comment">// file.fileName() returns the unique file name</span> } <span class="comment">// The QTemporaryFile destructor removes the temporary file</span> <span class="comment">// as it goes out of scope.</span> </pre> <p>Reopening a QTemporaryFile after calling <a href="qfile.html#close">close</a>() 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.</p> <p>The file name of the temporary file can be found by calling <a href="qtemporaryfile.html#fileName">fileName</a>(). Note that this is only defined after the file is first opened; the function returns an empty string before this.</p> <p>A temporary file will have some static part of the name and some part that is calculated to be unique. The default filename <tt>qt_temp</tt> will be placed into the temporary path as returned by <a href="qdir.html#tempPath">QDir.tempPath</a>(). If you specify your own filename, a relative file path will not be placed in the temporary directory by default, but be relative to the current working directory.</p> <p>Specified filenames can contain the following template <tt>XXXXXX</tt> (six upper case "X" characters), which will be replaced by the auto-generated portion of the filename. Note that the template is case sensitive. If the template is not present in the filename, QTemporaryFile appends the generated part to the filename given.</p> <hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QTemporaryFile" />QTemporaryFile.__init__ (<i>self</i>)</h3><p>Constructs a <a href="qtemporaryfile.html">QTemporaryFile</a> in <a href="qdir.html#tempPath">QDir.tempPath</a>(), using the file template "qt_temp.XXXXXX". The file is stored in the system's temporary directory.</p> <p><b>See also</b> <a href="qtemporaryfile.html#setFileTemplate">setFileTemplate</a>() and <a href="qdir.html#tempPath">QDir.tempPath</a>().</p> <h3 class="fn"><a name="QTemporaryFile-2" />QTemporaryFile.__init__ (<i>self</i>, QString <i>templateName</i>)</h3><p>Constructs a <a href="qtemporaryfile.html">QTemporaryFile</a> with a template filename of <i>templateName</i>. Upon opening the temporary file this will be used to create a unique filename.</p> <p>If the <i>templateName</i> does not contain XXXXXX it will automatically be appended and used as the dynamic portion of the filename.</p> <p>If <i>templateName</i> is a relative path, the path will be relative to the current working directory. You can use <a href="qdir.html#tempPath">QDir.tempPath</a>() to construct <i>templateName</i> if you want use the system's temporary directory.</p> <p><b>See also</b> <a href="qtemporaryfile.html#open">open</a>() and <a href="qtemporaryfile.html#fileTemplate">fileTemplate</a>().</p> <h3 class="fn"><a name="QTemporaryFile-3" />QTemporaryFile.__init__ (<i>self</i>, <a href="qobject.html">QObject</a> <i>parent</i>)</h3><p>The <i>parent</i> argument, if not None, causes <i>self</i> to be owned by Qt instead of PyQt.</p><p>Constructs a <a href="qtemporaryfile.html">QTemporaryFile</a> (with the given <i>parent</i>) in <a href="qdir.html#tempPath">QDir.tempPath</a>(), using the file template "qt_temp.XXXXXX".</p> <p><b>See also</b> <a href="qtemporaryfile.html#setFileTemplate">setFileTemplate</a>().</p> <h3 class="fn"><a name="QTemporaryFile-4" />QTemporaryFile.__init__ (<i>self</i>, QString <i>templateName</i>, <a href="qobject.html">QObject</a> <i>parent</i>)</h3><p>The <i>parent</i> argument, if not None, causes <i>self</i> to be owned by Qt instead of PyQt.</p><p>Constructs a <a href="qtemporaryfile.html">QTemporaryFile</a> with a template filename of <i>templateName</i> and the specified <i>parent</i>. Upon opening the temporary file this will be used to create a unique filename.</p> <p>If the <i>templateName</i> does not contain XXXXXX it will automatically be appended and used as the dynamic portion of the filename.</p> <p>If <i>templateName</i> is a relative path, the path will be relative to the current working directory. You can use <a href="qdir.html#tempPath">QDir.tempPath</a>() to construct <i>templateName</i> if you want use the system's temporary directory.</p> <p><b>See also</b> <a href="qtemporaryfile.html#open">open</a>() and <a href="qtemporaryfile.html#fileTemplate">fileTemplate</a>().</p> <h3 class="fn"><a name="autoRemove" />bool QTemporaryFile.autoRemove (<i>self</i>)</h3><p>Returns true if the <a href="qtemporaryfile.html">QTemporaryFile</a> 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 <a href="qtemporaryfile.html">QTemporaryFile</a> object on the stack, fill it with data, read from it, and finally on function return it will automatically clean up after itself.</p> <p>Auto-remove is on by default.</p> <p><b>See also</b> <a href="qtemporaryfile.html#setAutoRemove">setAutoRemove</a>() and <a href="qfile.html#remove">remove</a>().</p> <h3 class="fn"><a name="createLocalFile" /><a href="qtemporaryfile.html">QTemporaryFile</a> QTemporaryFile.createLocalFile (QString <i>fileName</i>)</h3><p>If <i>file</i> is not on a local disk, a temporary file is created on a local disk, <i>file</i> is copied into the temporary local file, and a pointer to the temporary local file is returned. If <i>file</i> is already on a local disk, a copy is not created and 0 is returned.</p> <h3 class="fn"><a name="createLocalFile-2" /><a href="qtemporaryfile.html">QTemporaryFile</a> QTemporaryFile.createLocalFile (<a href="qfile.html">QFile</a> <i>file</i>)</h3><p>This is an overloaded function.</p> <p>Works on the given <i>fileName</i> rather than an existing <a href="qfile.html">QFile</a> object.</p> <h3 class="fn"><a name="fileEngine" /><a href="qabstractfileengine.html">QAbstractFileEngine</a> QTemporaryFile.fileEngine (<i>self</i>)</h3><h3 class="fn"><a name="fileName" />QString QTemporaryFile.fileName (<i>self</i>)</h3><p>Returns the complete unique filename backing the <a href="qtemporaryfile.html">QTemporaryFile</a> object. This string is null before the <a href="qtemporaryfile.html">QTemporaryFile</a> is opened, afterwards it will contain the <a href="qtemporaryfile.html#fileTemplate">fileTemplate</a>() plus additional characters to make it unique.</p> <p><b>See also</b> <a href="qtemporaryfile.html#fileTemplate">fileTemplate</a>().</p> <h3 class="fn"><a name="fileTemplate" />QString QTemporaryFile.fileTemplate (<i>self</i>)</h3><p>Returns the set file template. The default file template will be called qt_temp and be placed in <a href="qdir.html#tempPath">QDir.tempPath</a>().</p> <p><b>See also</b> <a href="qtemporaryfile.html#setFileTemplate">setFileTemplate</a>().</p> <h3 class="fn"><a name="open" />bool QTemporaryFile.open (<i>self</i>)</h3><p>A <a href="qtemporaryfile.html">QTemporaryFile</a> will always be opened in <a href="qiodevice.html#OpenModeFlag-enum">QIODevice.ReadWrite</a> mode, this allows easy access to the data in the file. This function will return true upon success and will set the <a href="qtemporaryfile.html#fileName">fileName</a>() to the unique filename used.</p> <p><b>See also</b> <a href="qtemporaryfile.html#fileName">fileName</a>().</p> <h3 class="fn"><a name="open-2" />bool QTemporaryFile.open (<i>self</i>, <a href="qiodevice-openmode.html">QIODevice.OpenMode</a> <i>flags</i>)</h3><p>Reimplemented from <a href="qiodevice.html#open">QIODevice.open</a>().</p> <p>Creates a unique file name for the temporary file, and opens it. You can get the unique name later by calling <a href="qtemporaryfile.html#fileName">fileName</a>(). The file is guaranteed to have been created by this function (i.e., it has never existed before).</p> <h3 class="fn"><a name="setAutoRemove" />QTemporaryFile.setAutoRemove (<i>self</i>, bool <i>b</i>)</h3><p>Sets the <a href="qtemporaryfile.html">QTemporaryFile</a> into auto-remove mode if <i>b</i> is true.</p> <p>Auto-remove is on by default.</p> <p><b>See also</b> <a href="qtemporaryfile.html#autoRemove">autoRemove</a>() and <a href="qfile.html#remove">remove</a>().</p> <h3 class="fn"><a name="setFileTemplate" />QTemporaryFile.setFileTemplate (<i>self</i>, QString <i>name</i>)</h3><p>Sets the static portion of the file name to <i>name</i>. 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.</p> <p>If <i>name</i> contains a relative file path, the path will be relative to the current working directory. You can use <a href="qdir.html#tempPath">QDir.tempPath</a>() to construct <i>name</i> if you want use the system's temporary directory.</p> <p><b>See also</b> <a href="qtemporaryfile.html#fileTemplate">fileTemplate</a>().</p> <address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td align="left" width="25%">PyQt 4.10.3 for X11</td><td align="center" width="50%">Copyright © <a href="http://www.riverbankcomputing.com">Riverbank Computing Ltd</a> and <a href="http://www.qtsoftware.com">Nokia</a> 2012</td><td align="right" width="25%">Qt 4.8.5</td></tr></table></div></address></body></html>