<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- qtemporarydir.cpp --> <title>QTemporaryDir Class | Qt Core 5.9</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td >Qt 5.9</td><td ><a href="qtcore-index.html">Qt Core</a></td><td ><a href="qtcore-module.html">C++ Classes</a></td><td >QTemporaryDir</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right">Qt 5.9.4 Reference Documentation</td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#public-functions">Public Functions</a></li> <li class="level1"><a href="#details">Detailed Description</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">QTemporaryDir Class</h1> <!-- $$$QTemporaryDir-brief --> <p>The <a href="qtemporarydir.html">QTemporaryDir</a> class creates a unique directory for temporary use. <a href="#details">More...</a></p> <!-- @@@QTemporaryDir --> <div class="table"><table class="alignedsummary"> <tr><td class="memItemLeft rightAlign topAlign"> Header:</td><td class="memItemRight bottomAlign"> <span class="preprocessor">#include <QTemporaryDir></span> </td></tr><tr><td class="memItemLeft rightAlign topAlign"> qmake:</td><td class="memItemRight bottomAlign"> QT += core</td></tr></table></div><ul> <li><a href="qtemporarydir-members.html">List of all members, including inherited members</a></li> </ul> <p><b>Note:</b> All functions in this class are reentrant.</p> <a name="public-functions"></a> <h2 id="public-functions">Public Functions</h2> <div class="table"><table class="alignedsummary"> <tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qtemporarydir.html#QTemporaryDir">QTemporaryDir</a></b>()</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qtemporarydir.html#QTemporaryDir-1">QTemporaryDir</a></b>(const QString &<i>templatePath</i>)</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qtemporarydir.html#dtor.QTemporaryDir">~QTemporaryDir</a></b>()</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qtemporarydir.html#autoRemove">autoRemove</a></b>() const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> QString </td><td class="memItemRight bottomAlign"><b><a href="qtemporarydir.html#errorString">errorString</a></b>() const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> QString </td><td class="memItemRight bottomAlign"><b><a href="qtemporarydir.html#filePath">filePath</a></b>(const QString &<i>fileName</i>) const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qtemporarydir.html#isValid">isValid</a></b>() const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> QString </td><td class="memItemRight bottomAlign"><b><a href="qtemporarydir.html#path">path</a></b>() const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qtemporarydir.html#remove">remove</a></b>()</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qtemporarydir.html#setAutoRemove">setAutoRemove</a></b>(bool <i>b</i>)</td></tr> </table></div> <a name="details"></a> <!-- $$$QTemporaryDir-description --> <div class="descr"> <h2 id="details">Detailed Description</h2> <p>The <a href="qtemporarydir.html">QTemporaryDir</a> class creates a unique directory for temporary use.</p> <p><a href="qtemporarydir.html">QTemporaryDir</a> is used to create unique temporary directories safely. The directory itself is created by the constructor. The name of the temporary directory is guaranteed to be unique (i.e., you are guaranteed to not overwrite an existing directory), and the directory will subsequently be removed upon destruction of the <a href="qtemporarydir.html">QTemporaryDir</a> object. The directory name is either auto-generated, or created based on a template, which is passed to <a href="qtemporarydir.html">QTemporaryDir</a>'s constructor.</p> <p>Example:</p> <pre class="cpp"> <span class="comment">// Within a function/method...</span> <span class="type"><a href="qtemporarydir.html#QTemporaryDir">QTemporaryDir</a></span> dir; <span class="keyword">if</span> (dir<span class="operator">.</span>isValid()) { <span class="comment">// dir.path() returns the unique directory path</span> } <span class="comment">// The QTemporaryDir destructor removes the temporary directory</span> <span class="comment">// as it goes out of scope.</span> </pre> <p>It is very important to test that the temporary directory could be created, using <a href="qtemporarydir.html#isValid">isValid</a>(). Do not use <a href="qdir.html#exists-1">exists()</a>, since a default-constructed <a href="qdir.html">QDir</a> represents the current directory, which exists.</p> <p>The path to the temporary directory can be found by calling <a href="qtemporarydir.html#path">path</a>().</p> <p>A temporary directory will have some static part of the name and some part that is calculated to be unique. The default path will be determined from <a href="qcoreapplication.html#applicationName-prop">QCoreApplication::applicationName</a>() (otherwise <code>qt_temp</code>) and will be placed into the temporary path as returned by <a href="qdir.html#tempPath">QDir::tempPath</a>(). If you specify your own path, a relative path will not be placed in the temporary directory by default, but be relative to the current working directory. In all cases, a random string will be appended to the path in order to make it unique.</p> </div> <p><b>See also </b><a href="qdir.html#tempPath">QDir::tempPath</a>(), <a href="qdir.html">QDir</a>, and <a href="qtcore-attribution-qtemporaryfile.html#qtemporaryfile">QTemporaryFile</a>.</p> <!-- @@@QTemporaryDir --> <div class="func"> <h2>Member Function Documentation</h2> <!-- $$$QTemporaryDir[overload1]$$$QTemporaryDir --> <h3 class="fn" id="QTemporaryDir"><a name="QTemporaryDir"></a>QTemporaryDir::<span class="name">QTemporaryDir</span>()</h3> <p>Constructs a <a href="qtemporarydir.html">QTemporaryDir</a> using as template the application name returned by <a href="qcoreapplication.html#applicationName-prop">QCoreApplication::applicationName</a>() (otherwise <code>qt_temp</code>). The directory is stored in the system's temporary directory, <a href="qdir.html#tempPath">QDir::tempPath</a>().</p> <p><b>See also </b><a href="qdir.html#tempPath">QDir::tempPath</a>().</p> <!-- @@@QTemporaryDir --> <!-- $$$QTemporaryDir$$$QTemporaryDirconstQString& --> <h3 class="fn" id="QTemporaryDir-1"><a name="QTemporaryDir-1"></a>QTemporaryDir::<span class="name">QTemporaryDir</span>(const <span class="type"><a href="qstring.html">QString</a></span> &<i>templatePath</i>)</h3> <p>Constructs a <a href="qtemporarydir.html">QTemporaryDir</a> with a template of <i>templatePath</i>.</p> <p>If <i>templatePath</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>templatePath</i> if you want use the system's temporary directory.</p> <p>If the <i>templatePath</i> ends with XXXXXX it will be used as the dynamic portion of the directory name, otherwise it will be appended. Unlike <a href="qtcore-attribution-qtemporaryfile.html#qtemporaryfile">QTemporaryFile</a>, XXXXXX in the middle of the template string is not supported.</p> <p><b>See also </b><a href="qdir.html#tempPath">QDir::tempPath</a>().</p> <!-- @@@QTemporaryDir --> <!-- $$$~QTemporaryDir[overload1]$$$~QTemporaryDir --> <h3 class="fn" id="dtor.QTemporaryDir"><a name="dtor.QTemporaryDir"></a>QTemporaryDir::<span class="name">~QTemporaryDir</span>()</h3> <p>Destroys the temporary directory object. If auto remove mode was set, it will automatically delete the directory including all its contents.</p> <p><b>See also </b><a href="qtemporarydir.html#autoRemove">autoRemove</a>().</p> <!-- @@@~QTemporaryDir --> <!-- $$$autoRemove[overload1]$$$autoRemove --> <h3 class="fn" id="autoRemove"><a name="autoRemove"></a><span class="type">bool</span> QTemporaryDir::<span class="name">autoRemove</span>() const</h3> <p>Returns <code>true</code> if the <a href="qtemporarydir.html">QTemporaryDir</a> is in auto remove mode. Auto-remove mode will automatically delete the directory from disk upon destruction. This makes it very easy to create your <a href="qtemporarydir.html">QTemporaryDir</a> object on the stack, fill it with files, do something with the files, 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="qtemporarydir.html#setAutoRemove">setAutoRemove</a>() and <a href="qtemporarydir.html#remove">remove</a>().</p> <!-- @@@autoRemove --> <!-- $$$errorString[overload1]$$$errorString --> <h3 class="fn" id="errorString"><a name="errorString"></a><span class="type"><a href="qstring.html">QString</a></span> QTemporaryDir::<span class="name">errorString</span>() const</h3> <p>If <a href="qtemporarydir.html#isValid">isValid</a>() returns <code>false</code>, this function returns the error string that explains why the creation of the temporary directory failed. Otherwise, this function return an empty string.</p> <p>This function was introduced in Qt 5.6.</p> <!-- @@@errorString --> <!-- $$$filePath[overload1]$$$filePathconstQString& --> <h3 class="fn" id="filePath"><a name="filePath"></a><span class="type"><a href="qstring.html">QString</a></span> QTemporaryDir::<span class="name">filePath</span>(const <span class="type"><a href="qstring.html">QString</a></span> &<i>fileName</i>) const</h3> <p>Returns the path name of a file in the temporary directory. Does <i>not</i> check if the file actually exists in the directory. Redundant multiple separators or "." and ".." directories in <i>fileName</i> are not removed (see <a href="qdir.html#cleanPath">QDir::cleanPath</a>()). Absolute paths are not allowed.</p> <p>This function was introduced in Qt 5.9.</p> <!-- @@@filePath --> <!-- $$$isValid[overload1]$$$isValid --> <h3 class="fn" id="isValid"><a name="isValid"></a><span class="type">bool</span> QTemporaryDir::<span class="name">isValid</span>() const</h3> <p>Returns <code>true</code> if the <a href="qtemporarydir.html">QTemporaryDir</a> was created successfully.</p> <!-- @@@isValid --> <!-- $$$path[overload1]$$$path --> <h3 class="fn" id="path"><a name="path"></a><span class="type"><a href="qstring.html">QString</a></span> QTemporaryDir::<span class="name">path</span>() const</h3> <p>Returns the path to the temporary directory. Empty if the <a href="qtemporarydir.html">QTemporaryDir</a> could not be created.</p> <!-- @@@path --> <!-- $$$remove[overload1]$$$remove --> <h3 class="fn" id="remove"><a name="remove"></a><span class="type">bool</span> QTemporaryDir::<span class="name">remove</span>()</h3> <p>Removes the temporary directory, including all its contents.</p> <p>Returns <code>true</code> if removing was successful.</p> <!-- @@@remove --> <!-- $$$setAutoRemove[overload1]$$$setAutoRemovebool --> <h3 class="fn" id="setAutoRemove"><a name="setAutoRemove"></a><span class="type">void</span> QTemporaryDir::<span class="name">setAutoRemove</span>(<span class="type">bool</span> <i>b</i>)</h3> <p>Sets the <a href="qtemporarydir.html">QTemporaryDir</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="qtemporarydir.html#autoRemove">autoRemove</a>() and <a href="qtemporarydir.html#remove">remove</a>().</p> <!-- @@@setAutoRemove --> </div> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2017 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.<br> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. </p> </div> </body> </html>