QT from entry to soil -- file reading and writing operation

introduction
File reading and writing is a function of many applications, and even some applications are developed around the processing of files in a certain format, so file reading and writing is a basic function of application development.

Qt provides two basic methods for reading and writing plain text files:

Use the IODevice read-write function of QFile class to read and write directly
QFile and QTextStream are combined to read and write files by stream method.
1, File read operation
(1) Using the QFile class

Qt encapsulates the QFile class to facilitate our file operation. You can follow the following steps:

Loading file objects using QFile
Open the file file Open (open mode)
Operation file
Close the file file close()
Example: click the read / write file button to read the file contents into textEdit

one ️⃣ Set ui interface

two ️⃣ In widget Edit the code in CPP (the QFileDialog class is an open file)

 //Click the select File button to open the file dialog box
    connect(ui->pushButton,&QPushButton::clicked,[=](){
    QString path=  QFileDialog::getOpenFileName(this,"Open file","C:/Users/WFD/Desktop");
   //Displays the path in lineEdit
   ui->lineEdit->setText(path);

      //Read the contents of the file and put it into textEdit
      QFile file(path);//Parameter is the path of the file
      //Set opening method
      file.open(QIODevice::ReadOnly);
      //Use the QByteArray class to receive the read information
      QByteArray array=file.readAll();
      //Put the read data into textEdit
      ui->textEdit->setText(array);
      //Close file object
      file.close();

Note: when setting the opening mode

The QFile::open() function needs to pass the parameter of QIODevice::OpenModeFlag enumeration type when opening a file to determine how to open the file. The main values of QIODevice::OpenModeFlag type are as follows:

QIODevice::ReadOnly  //Open the file as read-only for loading.
QIODevice::WriteOnly  //Open a file in write only mode to save the file.
QIODevice::ReadWrite //Open in read-write mode.
QIODevice::Append  //Open in add mode, and the data newly written to the file is added to the end of the file.
QIODevice::Truncate //Open the file by intercepting, and all the original contents of the file will be deleted.
QIODevice::Text       //When the file is opened in text mode, the "\ n" is automatically translated into line feed when reading, and the end character of the string will be automatically translated into the code of the system platform when writing, such as "\ r\n" on Windows platform.

These values can be combined, for example QIODevice::ReadOnly | QIODevice::Text Indicates that the file is opened as read-only and text.

Note: when working with files

Open a text file in read-only mode, and then use the readAll() method to read out the contents of the file at one time. The return value is the byte array QByteArray. Qtring is used to store binary data if you want to convert qtring to binary data. (sometimes the system will automatically switch)

We can also use the readLine method to read one line at a time, and then operate on one line at a time: (use file.atEnd to judge whether to read the last line)

QByteArray array;
  while(!file.atEnd())
  {
       array+=file.readLine();//+=Overlay read lines
  }

(2) Using the QTextStream class

If the operation is a text file, Qt also specifically encapsulates a class for processing text stream, which we can use to read text content

  //Click the select File button to open the file dialog box
    connect(ui->pushButton,&QPushButton::clicked,[=](){
      QString path=  QFileDialog::getOpenFileName(this,"Open file","C:/Users/WFD/Desktop");
      //Place the path in lineEdit
      ui->lineEdit->setText(path);
      //Read the contents of the file and put it into textEdit
      QFile file(path);//Parameter is the path of the file
      //Set opening method
      file.open(QIODevice::ReadOnly);
      //Use QTextStream class to read text information
     QTextStream QS(&file);
     //Use QString class to receive the read information
     QString array=QS.readAll();
      //Put the read data into textEdit
      ui->textEdit->setText(array);
      //Close file object
      file.close();

2, File write operation

(1) Using the QFile class
You can also write to a file using QFile:

(2) Using the QTextStream class
QTextStream class overloads the operator. We can flow the string into the text file through < < operator:

3, File information reading
In addition to reading and writing files, Qt also encapsulates the QFileInfo class to help us obtain file metadata, such as file size, suffix, creation time, last modification time, etc

expand:

Each code conversion

QString -> QByteArray      QString.toUtf8();

QByteArray -> std::string  QByteArray.toStdString();

std::string -> char *        string.date();

Common static functions

QFileDialog::getOpenFileName()    //Gets the specified file pathname and returns QString
QFileDialog::getExistingDirectory()  //Gets the QString returned by the specified path
QFileDialog::getSaveFileName()    //Get the specified save path name and return QString

Establishment, reading and writing of QT configuration ini file

1. Introduction to INI file

. ini file is the abbreviation of Initialization File, that is, Initialization File.

In addition to windows, there are many application software under other operating systems ini file is used to configure the application software to meet the requirements of different users. Generally, you don't need to edit these directly ini file, the graphical interface of the application can be operated to achieve the same function. It can be used to store software information, registry information, etc.

2. INI file format

The INI file consists of sections, keys, and values.

section

  [section] 

Parameter (key)=Value)

name=value

The following is an example of an ini file

[Section1 Name]   

KeyName1=value1   

KeyName2=value2   

...   

[Section2 Name]   

KeyName21=value21   

KeyName22=value22 

Where: [Section1 Name] is used to represent a paragraph. Because the INI file may be shared in the project, the [Section Name] Section Name is used to distinguish parameter areas for different purposes. For example: [Section1 Name] indicates the sensor sensitivity parameter area; [Section2 Name] indicates the measurement channel parameter area, etc.

Note: use semicolons to indicate (;). The text after the semicolon is annotated until the end of the line.

3 Qt write ini file

#include <QApplication>
#include <QSettings>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    //Qt uses QSettings class to read and write ini files
    //The first parameter of the QSettings constructor is the path to the ini file. The second parameter indicates the path to the ini file. The third parameter can be defaulted
    QSettings *configIniWrite = new QSettings("hahaya.ini", QSettings::IniFormat);
    //Write the contents to the ini file. The two parameters of the setValue function are key value pairs
    //Write content to the first section of the ini file and the first parameter under the ip section
    configIniWrite->setValue("/ip/first", "192.168.0.1");
    //Write content to the first section of the ini file and the second parameter under the ip section
    configIniWrite->setValue("ip/second", "127.0.0.1");
    //Write content to the second section of the ini file and the first parameter under the port section
    configIniWrite->setValue("port/open", "2222");
    //Delete pointer after writing
    delete configIniWrite;
    return a.exec();
}

After running the program, open hahaya.com under the program directory INI file, the result is shown in the following figure:

4 Qt read ini file

#include <QApplication>
#include <QSettings>
#include<QDebug>
int main(int argc, char *argv[])
{
      QApplication a(argc, argv);
      QSettings *configIniRead = new QSettings("hahaya.ini", QSettings::IniFormat);
      //Save the read ini file in QString, take the value first, and then convert it to QString type through toString() function
      QString ipResult = configIniRead->value("/ip/second").toString();
      QString portResult = configIniRead->value("/port/open").toString();
      //Print the results
      qDebug() << ipResult;
      qDebug() << portResult;
      //Delete pointer after reading
      delete configIniRead;
      return a.exec();
}

Keywords: C++ OpenCV Qt UI

Added by Sirus121 on Sun, 16 Jan 2022 00:57:43 +0200