How to Save a File in a Qt Widget Application in C++


C++


In this article, we show how to save a file in a Qt widget application in C++.

In this program, we create a simple text editor and we have a button in which we can save whatever we wrote on a text editor, simple to many word processing softwares, such as Notepad, Wordpad, or Microsoft Word.

This is shown below.

Saving a file in a Qt widget in C++

So you can see the 'Save As' button on the right side of the application. Once this button is pressed, it opens up a standard window that allows you to choose a file name to save the file as. We also add in extra features such as the file not saving if nothing is entered into the filename. This way, the file must have a filename associated with it.

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, 'Save As', as it functions as a 'Save As' button.

We change the objectName to 'SaveAsButton' 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 save 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 save files.

We need the QFileDialog class in order to have the file dialog window open up when we click the 'Save As' button in our program. This allows us to either create a new file or search our computer for an existing file (such as to overwrite its contents).

The QTextStream class allows us to take the contents from the textEdit object and write it to the new file we are creating.

So we create a function, void Widget::on_SaveAsButton_clicked(), which is executed when the 'Save As' button is clicked.

We do a number of things within this function.

We create a variable, of type String, named filename. We take the filename from the file dialog window and save it to this filename variable.

We want to do a number of things when checking this.

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

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 WriteOnly or Text.

If the file cannot be opened, then we do not save it.

For this basic program, our code really only deals with new blank projects because we haven't created a button in this program which allows us to select an existing file to open up a file that has existing content. However, this can easily be done, which allows us to open up and modify an existing file.

Next, we create an QTextStream object named out, which allows us to write the contents from the textEdit element to 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 write the contents of the textEdit element to the file through the line, out << ui->textEdit->toPlainText() << "\n";

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

So this is what we can do for saving a basic text file.

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

And this is how to save a file in a Qt widget in C++.


Related Resources

How to Write to a File in C++

HTML Comment Box is loading comments...