How to List the Contents of a Directory in a Qt Widget Application in C++


C++


In this article, we show how to list the contents of a directory in a Qt widget application in C++.

So a directory, unless empty, has items in it, files and other directories.

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

Listing the contents of a directory in a Qt widget application in C++

So you can see the Documents directory is made up of several files and directories.

How can we list the contents of 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.

To creat 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.

We do a number of things within this function.

The first thing we want to do is make sure the listWidget is cleared. So any existing directory's contents that is shown from possibly a previous selection is deleted.

We then create a variable of type string named dirPath, which takes the text from the line edit element and stores it. This represents the directory you selected.

We then make sure that the path isn't empty.

We then create a variable of type QDir, which represents the directory on the computer file system.

We then create another variable, fileList, which stores all of the contents (files and directories) of a directory.

We then create a for loop so that we can list each element of the fileList variable. We begin at element 0, since all lists in programming begin at element 0 and we go up to fileList.size(), which represents the amount of objects in the folder. We then add each element to the list widget using the addItem() function. We write the absolute file path for each element in the directory.

If you want to explicitly show which elements are files and which are directories, you can can modify the above program to contain the following below.



This creates a prefix to show which object is a file and which is a directory. This usually isn't necessary but you may use this basic setup to create a more advanced file listing system that shows an directory image icon next to a directory and a file image icon next to a file, if this is what you desire.

And this is how to list the contents of 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...