QT file reading and writing practical tutorial

QT practical tutorial:

The source address of this article: QT file reading and writing

file dialog

Before understanding QTthe 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 lineTitleand 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 QFileDialogis the file dialog, which has the effect of

insert image description here

read file

QTThe class is encapsulated QFilefor 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 opento open the file readAllto read the content. The result is

insert image description here

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 ReadOnlymeans read-only, Textmeans open as text.

file.openReturn 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 clickedaction, 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, getSaveFileNameis a save dialog box, which is very convenient for storage operations.

file.writeThere are three overloads, but QStringthe input is not supported, so it is used toUtf8to convert it to a string.

The result is shown in the figure

insert image description here

According to the above figure, it can be seen that these contents are stored in the file named test.txt.

copy and cut

QFileIn addition to implementing functions such as file reading and writing, it also provides some functions for directly manipulating files. For example copy, rename, removeetc. are used for copying, renaming, and deleting as the names suggest.

Among them, renameit 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

insert image description here

Guess you like

Origin blog.csdn.net/m0_37816922/article/details/124336122