What's New in Qt 5: The QTemporaryDir Class

By ICS | Tuesday, January 22, 2013

One sometimes needs to store information in a temporary location. POSIX compliant platforms like Linux provide the tmpfile() library function to create a unique temporary file that is automatically deleted when it is closed or the program terminates. Qt provides an abstraction of this with the QTemporaryFile class.

Qt 5.0.0 introduces a new QTemporaryDir class that can create a temporary directory. The directory name is guaranteed to be unique and gets created by the constructor and removed in the destructor. Typically you create a QTemporaryDir on the stack so that it gets deleted when it falls out of the scope in which it was created. The behavior of automatically deleting the directory can be disabled if desired using a method call.

The class is quite simple and only includes two constructors, a destructor, and five methods.

Let's look at a simple but complete example program that illustrates using the QTemporaryDirclass. To keep it simple the example doesn't create a QApplication instance as a real GUI application would, but for this simple example we don't need the Qt event loop.

The full listing is at the end of this posting. Let's note a few items of interest in the code.

The QTemporaryDir is created in it's own scope (block). It gets deleted when execution reaches the end of that block. We used the constructor that accepts a QString argument. This allows specifying a template to specify the non-unique portion of the directory name. Any files in the directory get deleted when the QTemporaryDir is removed.

To make the example a little more realistic we create a file in the directory and write some text to it using standard QFile and QTextStream classes.

Sample output of the program should look like this:

Created temporary directory Directory is valid Path is "my-namedIZIaW" AutoRemove is set to true Directory exists Created file in directory Now outside scope of temporary directory Directory does not exist

The program is simple enough that the qmake project file can be created using "qmake -project".

Here is one final handy tip for projects like this that require Qt 5: Adding the following line to your qmake project file will cause it to produce a meaningful error message if someone tries to build the project with Qt 4:

lessThan(QT_MAJOR_VERSION, 5): error(This project requires Qt 5 or later)

The complete code listing is below:

#include <QDebug>
#include <QDir> 
#include <QFile> 
#include <QTemporaryDir> 
#include <QTextStream> 
/* Example of Qt 5 QTemporaryDir class. */ 

int main() { 
  QDir dir; 
    // Create temporary directory. 

    QTemporaryDir tempDir("my-name"); 
    qDebug() << "Created temporary directory"; 
    if (tempDir.isValid()) { 
      qDebug() << "Directory is valid"; 
      qDebug() << "Path is" << tempDir.path(); 
      qDebug() << "AutoRemove is set to" << tempDir.autoRemove(); 
      if (dir.exists()) qDebug() << "Directory exists"; 
      else qDebug() << "Directory does not exist"; 
      // Create a file in the directory and write some data to it. 
      QFile file(tempDir.path() + "/testfile.txt"); 
      if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        qDebug() << "Created file in directory"; 
        QTextStream out(&file); out << "Hello, world!\n"; 
      } else { 
        qDebug() << "Unable to create file in directory"; 
    } else {
      qDebug() << "Directory is not valid";

  // Outside of scope, tempDir should be deleted. 

  qDebug() << "Now outside scope of temporary directory";
  if (dir.exists()) qDebug() << "Directory exists"; 
  else qDebug() << "Directory does not exist"; 

Have a question or add to the conversation: Log in Register