How to Add Mulitple File Filters to a Qt Widget Application in C++
In this article, we show how to add multiple file filters to a Qt widget application in C++.
When working with QFileDialog objects, we can open a file dialog object and only show files within a certain directory with specific file types.
We do this by adding file filters. We can add multiple file filters to a file dialog.
For example, a person could be uploaded a document and we can add multiple file filters that accepts the document as a PDF, a Microsoft Word document, or a simple text file. With each selection, each option has a label indicating the type of file and only files of that filter are shown when that item is selected.
This is shown below.
You can see that we have added multiple file filters to the Qt widget application.
In this case, PDFs, Microsoft Word Documents, and Text Files.
If we select PDFs, only files ending in .pdf will be shown.
If we select Microsoft Word documents, only files ending in .doc or .docx will be shown.
If we select Text Files, only files ending in .txt or .rt will be shown.
Thus, we have created multiple file filters to make selecting a file easier in ways. We only give more structure and guidance to the user this way.
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 File', as it functions to select a file. We give this element an objectName of 'chooseFileButton'.
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 File' button in our program. This allows us to search our computer for an existing directory.
So we create a function, void Widget::on_chooseFileButton_clicked(), which is executed when the 'Choose File' button is clicked.
Within this function, we have created 3 file filters: PDFs, Microsoft Word Documents, and Text Files.
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, "PDFs", "Microsoft Word Documents", and "Text Files"
Within this same attribute you specify all the file types that you want visible for each option. "PDFs" only shows files ending in .pdf. "Microsoft Word Documents" only shows files ending in .doc or .docx. "Text Files" only shows files ending in .txt or .rtf.
We then output the full path of the file selected.
Any number of file filters can be added to a QFileDialog object.
This allows a programmer or software designer to provide the proper selections of files that are
desired for a certain application.
And this is how to add multiple file filters to a Qt widget application in C++.
Related Resources