QT practical tutorial:
The source address of this article: QT file reading and writing
Article directory
file dialog
Before understanding QT
the file reading and writing function in detail, you can first implement a small function, that is, select a file and display the file name.
First drag three controls on the design interface, namely pushButton, lineEdit and textBrowser. Rename the latter two to lineTitle
and txtContent
, then add a slot for pushButton, that is, right->go to slot->click(), and then add in the automatically generated code
#include "qfiledialog.h"
void MainWindow::on_pushButton_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this,"请选择文件","/");
ui->lineTitle->setText(fileName);
}
where QFileDialog
is the file dialog, which has the effect of
read file
QT
The class is encapsulated QFile
for the input and output of the file. The path of the file needs to be input during construction, and then the member function can be used open
to open the file readAll
to read the content. The result is
code show as below
void MainWindow::on_pushButton_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this,"请选择文件","/");
ui->lineTitle->setText(fileName);
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
ui->txtContent->setText("文件打开失败");
else
ui->txtContent->setText(file.readAll());
}
Which ReadOnly
means read-only, Text
means open as text.
file.open
Return if successful true
, otherwise return false
.
write to file
Next, implement a function - save the file just read as another file.
First drag a pushButton control and rename it btnSaveAs
, then add an clicked
action, the code is
void MainWindow::on_btnSaveAs_clicked()
{
QString fileName = QFileDialog::getSaveFileName(this,"请输入文件名");
ui->lineTitle->setText(fileName);
QFile file(fileName);
if(!file.open(QIODevice::WriteOnly|QIODevice::Text))
ui->lineTitle->setText("文件保存失败");
else{
QString txt = ui->txtContent->toPlainText();
file.write(txt.toUtf8());
ui->lineTitle->setText(fileName);
}
}
Among them, getSaveFileName
is a save dialog box, which is very convenient for storage operations.
file.write
There are three overloads, but QString
the input is not supported, so it is used toUtf8
to convert it to a string.
The result is shown in the figure
According to the above figure, it can be seen that these contents are stored in the file named test.txt
.
copy and cut
QFile
In addition to implementing functions such as file reading and writing, it also provides some functions for directly manipulating files. For example copy
, rename
, remove
etc. are used for copying, renaming, and deleting as the names suggest.
Among them, rename
it can also play the role of cutting. Next, I will demonstrate that a new button is created, and then the function is triggered when it is clicked.
void MainWindow::on_btnCutFile_clicked()
{
QString oldName = QFileDialog::getOpenFileName(this,"请选择文件");
QString newName = QFileDialog::getSaveFileName(this,"请输入文件名");
QFile::rename(oldName,newName);
ui->txtContent->setText("file moved from"+oldName+"to"+newName);
}
to get the effect