How to Create a New Directory in a Qt Widget Application in C++


In this article, we show how to create a new diretory on a computer file system in a Qt widget application in C++.

So creating a directory is very standard in many programs. Many programs allow for the creation of new directories on a computer. We can then place files or additional directories within this newly created directory.

How can we create a directory on our computer in a Qt widget application?

Below is the program where we create a directory by typing the full path within the line edit element and then click the 'Create Directory' button. If you then check the directory in which you created this new directory, you should be able to see it.

Creating a new directory in a qt widget in c++

So you can see the 'Create Directory' button on the right side of the application. Once this button is pressed, a new directory is created according to the path specified in the line edit element.

So the first thing you have to do is create a Qt widget application.

You will have to place 1 line edit element and 2 push buttons within this widget application.

We label one push button, 'Choose Directory', as it functions to select a file. We give this element an objectName of 'chooseDirectoryButton'.

We label another push button, 'Create Directory', as it functions to create a new directory. We give this element an objectName of 'createDirectoryButton'.

With this, we now go to the heart of our code found in the 'widget.cpp' file.

Within this 'widget.cpp' file, we place the following contents shown below.

So we create a function, void Widget::on_createDirectoryButton_clicked(), which is executed when the 'Create Directory' button is clicked.

We do a number of things within this function.

We create a variable, of type String, named dirPath. Into this variable, we will store the full path of the directory. The text() function takes the path specified in the line edit element and stores it into this variable.

We make sure that the dirPath variable isn't empty by using an if statement with the isEmpty() function.

We then create a directory. This is done by using the QDir keyword and specifying a variable name, in this case, dir. We pass into this dir variable the full path to the directory we want to create. This creates a directory with the path that is specified within the line edit element.

We then want to make sure that the directory doesn't exist. So we use an if statement. If the directory doesn't exist, then we can create it, because it doesn't conflict with any existing directory.

You can create a directory with the direct line, dir.mkpath(dirPath);

However, if you want to output a message that the directory has been successfully created or not, then we can use if statements to do this.

We use the QMessageBox class to output if the directory was successfully created or not.

And this is how to create a new directory in a Qt widget in C++.

Related Resources

How to Write to a File in C++

HTML Comment Box is loading comments...