How to Determine if an Item is a File or Directory in a Qt Widget Application in C++


C++


In this article, we show how to determine if an item is a file or a directory in a Qt widget application in C++.

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

How can we determine if an item is a file or a directory in a Qt widget in C++?

To determine if an item is a file, we use the isFile() function.

To determine if an item is a directory, we use the isDir() function.

We use these functions on the specific item that we are targeting.

Below is a Qt widget application which shows the contents of a directory, classifying each item in the directory as a file or a directory with the appropriate prefixes.

Classifying an item as a file or directory in a Qt widget application in C++

You can see that each item in the directory is classified as either a file or a directory.

So let's now do this.

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 then have a for loop which loops through all items in our directory.

This is where we check to see whether each item is a file or a directory.

We create a variable of type QString, prefix.

This will allow us to place a prefix before each item in a directory to indicate whether the item is a file or a directory.

Each item is addressed by, fileList.at(i)

We use the isFile() function to determine if the item is a file. If it is, we give it a prefix of "File: "

We use the isDir() function to determine if the item is a directory. If it is, we give it a prefix of "Directory: "

We then output the prefix and the full path of the item in the directory by the line, ui->listWidget->addItem(prefix + fileList.at(i).absoluteFilePath());

And this is how to determine if an item is a file or 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...