How to Find the Number of Items in a Directory in a Qt Widget Application in C++


C++


In this article, we show how to find and display the number of items in a directory in a Qt widget application in C++.

So a directory, unless empty, has items in it, composed of files or other directories. Files can be anything such as Microsoft Word documents, text files, image files, etc. Directories are other directories within the directory.

Below is the program that we create that lists the contents of the Documents directory on a computer file system.

Displaying the number of items in a directory in a Qt widget application in C++

So you can see that there are 13 items in the Documents directory.

How can we find and display the number of items in a directory in a Qt widget application in C++?

Our program works by specifying a directory on the line edit element and then clicking the 'Show Folder Contents' button. All of the files and directories of the directory will then be shown, along with the number of items in the directory at the bottom of the list widget.

To create this, the first thing you have to do is create a Qt widget application.

You will have to place 1 line edit element, 1 list widget, and 1 push button within this widget application.

We label the push button, 'Show Folder Contents', as it functions to show the contents of a directory, or folder. We give this element an objectName of 'showFolderContentsButton'.

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.

We also put in a 'Choose Directory' button to make it easier to select a directory from your computer file system.



So let's now go over the code.

So we create a function, void Widget::on_showFolderContentsButton_clicked(), which is executed when the 'Show Folder Contents' button is clicked.

Within this function, we first clear the list widget element of any contents it may have to create a clean slate.

We then take the path specified within the line edit element and store it within a variable of type string, dirPath, which will store the path.

We then check to make sure the path isn't empty.

We then create a variable of type QDir, named dir, which represents the selected directory we are working with.

We then create a variable of type QList, named fileList, which stores all the files and directories in the selected directory. This is done using the entryInfoList() function.

We create an integer named, dirsize, which stores the number of items in the directory, which includes both files and directories.

In order to add this item to a list widget, we must convert the variable from type int to a string. We do this with the line, QString::fromStdString(std::to_string(dirsize));

We then have a for loop which loops through all items in our directory. This gives the full absolute path of each file.

We then create a line break by adding in the line, ui->listWidget->addItem("");

We then show how many items there are in the directory by adding the line, ui->listWidget->addItem("There are " + filesize + " items in this directory");

If you wanted to create a more advanced interface that displays how many files there are and how many directories there are, the following code shown below can be used.

Below we show the modified void Widget::on_showFolderContentsButton_clicked() function.



So we added a few more variables to keep track of which items are files in the directory and which are directories in the directory. numberoffiles and numberofdirectories are variables initially set to 0, which keep track of the number of files and number of directories in the directory, respectively.

We use the isFile() function to check if the item is a file. If it is, we add 1 to the numberoffiles variable.

We use the isDir() function to check if the item is a directory. If it is, we add 1 to the numberofdirectories variable.

The for loop loops through all items in the directory. The numberoffiles and numberofdirectories are updated throughout the prcess.

Once we are out of the for loop, we create new variables, numfiles and numdirectories, to store the converted int value of numberoffiles and numberofdirectories to a string value, so that it can be used with the addItem() function used with list widgets.

We then show the number of files and the number of directories using the line, ui->listWidget->addItem(numfiles + " files, " + numdirectories + " directories");

The result of this is shown below.

Displaying the number of items in a directory showing 
the number of files and directories in a Qt widget application in C++

You can see in the Documents directory above, we have 4 files and 9 directories within this directory.

And this is how to find the number of items in a directory in a Qt widget application in C++.


Related Resources

How to Write to a File in C++



HTML Comment Box is loading comments...