How to Select and Open a File on a Computer in a Qt Widget Application in C++


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

So when you work with various word processing softwares, there is always an option to Select a File. There is generally an 'Open' element, which when selected, opens up a file dialog window which allows you to select a file on your operating system.

In this program, we create a Qt widget which contains a 'Open File' button.

This is shown below.

Selecting and opening a file in a Qt widget in C++

So you can see the 'Open File' button on the right side of the application. Once this button is pressed, it opens up a standard file dialog window that allows you to select and open a file.

Once the file is chosen, its content can then be read and the file can be modified and saved, just like any word processing software.

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

You will have to place 2 elements within this widget application: a text edit and a push button.

We label the push button, 'Open File', as it functions as a 'Open File' button.

We change the objectName to 'SelectFileButton' to make it more descriptive.

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 open a file on your computer, we need to include a few classes.

We need the QFile class in order to work with files on your computer. This allows us to do things such as open and save files.

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

The QTextStream class allows us to take the contents from the file object and write it to the textEdit element of our program so that we can read and/or modify its contents.

So we create a function, void Widget::on_SelectFileButton_clicked(), which is executed when the 'Open File' button is clicked.

We do a number of things within this function.

We create a variable, of type String, named fileContent. Into this variable, we will store the contents of the file that we select.

Next, we create another variable, filename, which stores the the file that we select.

We don't want an empty or blank filename. Therefore, we check if the filename is empty. If it is, the program doesn't continue.

We then want to check if the file successfully opens or not when we access it.

We reference the file through the line, QFile file(filename);

The file variable now refers to the file we have selected through the file dialog window.

We now use an if statement to check if the file does not successfully open. We have a few OR statements because the file could be in several different modes such as ReadWrite or Text.

If the file cannot be opened, then the program does not continue.

Next, we create an QTextStream object named in, which allows us to read the contents from the file.

&file refers to the address, or location, of the file, which the program then writes the contents of the textEdit element to.

We then read the contents of the file and write its contents to the textEdit element through the line, fileContent= in.readAll();

We then close the file, which should be done now that we are finished reading from it.

We then make sure that we clear the textEdit element of any contents that may be in it and write the contents of the selected file to the textEdit element using the setPlainText() function, passing in the variable, fileContent, as its parameter.

So this is what we can do for selecting and opening a basic text file.

Notice that we write the contents of the file to the textEdit element using the setPlainText() function. This is valid for plain text editors. If you need a more advanced editor, then you wouldn't use the function setPlainText(). You would have to see what function best fits for the type of file you are working with.

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

Related Resources

How to Write to a File in C++

HTML Comment Box is loading comments...