How to Select and Open Specific File Types in a Qt Widget Application in C++


In this article, we show how to select and open specific file types in a Qt widget application in C++.

When working with QFileDialog objects and using the getExistingDirectory() function, we can open a file dialog object and only show files within a certain directory with specific file types.

This is a useful function in many various applications.

An example is an application asking a user for a photo. If you have a button for a user to search their computer system, you want the files to only show image file types, such as jpg, png, etc.

You don't want file types such as microsoft word documents or .exe files or any other type of files to be shown other than image file types.

Below is the program application that we create that has a 'Chooose Image' button. Upon being pressed, it only shows files that are either jpg or png files.

Selecting and opening specific file types in a qt widget in c++

You can see that only images are showing when this button is pressed, which is the intended effect.

This again can be done for image files or for any type of file.

Imagine this was an application for a school in which a student could only submit a Microsoft Word document. Thus, only files with the extensions of .doc or .docx would be allowed.

If only PDF files were allowed, only documents with an extension of .pdf would be accepted.

So how do we do this in a Qt widget application?

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

You will have to place 1 push button within this widget application.

We label one push button, 'Choose Image', as it functions to select an image file. We give this element an objectName of 'chooseImageButton'.

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.

In order to select a file on a user's local computer, we need to include the QFileDialog class.

We need the QFileDialog class in order to have the file dialog window open up when we click the 'Choose Image' button in our program. This allows us to search our computer for an existing directory.

So we create a function, void Widget::on_chooseImageButton_clicked(), which is executed when the 'Choose Image' button is clicked.

Within this function, we only only files to be visible that are either .png or .jpg files. Once the file is selected, we output the full path of the selected file in the Debug window.

The function to select files is the getOpenFileName() function.

Within the tr attribute, you specify the text that you want users to see, in this case, it is, "Images"

Within this same attribute you specify all the file types that you want visible when this directory is opened, in this case, .png and .jpg

We then output the full path of the file selected.

So again, this can be applied to any type of file extension so that file type selection can be limited to the desired file type wanted. This could be Microsoft Word documents, PDF files, image files, video files, etc.

This allows a programmer or software designer to force the proper selection of a file that is desired for a certain application.

And this is how to select and open specific file types in a Qt widget in C++.

Related Resources

How to Write to a File in C++

HTML Comment Box is loading comments...