How to Copy a File in a Qt Widget Application in C++
In this article, we show how to copy a file on a computer in a Qt widget application in C++.
When we talk about copying files, we mean making an exact replica of the file, of the same file type and file content.
Below is the program where we select a file and then copy the file using a different filename.
So you can see the 'Select File' button on the right bottom side of the application. Once this button is pressed, it opens up a standard file dialog window that allows you to select a file.
Once the file is chosen, its full path then appears on the line edit element.
You can then copy this path and change the filename and press the 'Copy' button. Now when you check the folder, you should see the new copied file in this directory. This file should have the same exact contents as the original file.
So the first thing you have to do is create a Qt widget application.
You will have to place 2 line edit elements and 2 push buttons within this widget application.
We label one push button, 'Select File', as it functions to select a file. We give this element an objectName of 'selectFileButton'.
We label the other push button, 'Copy', as it functions to copy a file. We give this element an objectName of 'copyFileButton'.
We give one line edit element the objectName of 'selectedFile', since this represents the original file that you select.
We give the other line edit element the objectName of 'copiedFile', since this represents the copied file.
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 and make a copy of 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 'Select File' button in our program. This allows us to search our computer for an existing file.
So we create a function, void Widget::on_selectFileButton_clicked(), which is executed when the 'Select File' button is clicked.
We do a number of things within this function.
We create a variable, of type String, named filename. Into this variable, we will store the full path of the file.
We make sure that the filename isn't empty by using an if statement with the isEmpty() function.
We then write the full file path to the line edit element.
We next create another function, void Widget::on_copyFileButton_clicked(), which is executed when the 'Copy' button is clicked.
This allows us to copy the file that we select.
The first thing we have to do is collect the filenames in each line edit element.
This serves two purposes: to make sure the elements aren't empty and to know the path and file name is for the new copied file.
The first variable, selectedFile, represents the full path of the file that we select.
The second variable, copiedFile, represents the full path of the file that we copy.
We make sure that these elements aren't empty.
We then create a file object for our original selected file, which we name, file.
We then make a copy of this file using the copy() function.
Within this if statement, we create a message box that appears that tells us that this file has been successfully copied.
And this is how to copy a file in a Qt widget in C++.
Related Resources