Qt--Text Flow and Data Flow, Buffer Operation and Data Operation

1. Text Flow and Data Flow

The file types mentioned in A.Qt are divided into two main categories
1. Text File - The contents of the file are readable text characters
2. Data Files - File contents are direct binary data
QFile directly supports reading and writing text files and data files

B.Qt provides auxiliary classes to simplify reading and writing text/data files
1.QTextStream -- All written data is converted to readable text
2.QDataStream -- Written data converted to binary data by type
How to use IO device auxiliary classes

Different Qt versions of streaming files may have different formats
void setVersion(int v)//Set version number
int version()const//Get Read-Write Version Number
Code Samples

#include <QCoreApplication>
#include <QFile>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QFile file("C:/Users/59673/Desktop/test.hex");

    if( file.open(QIODevice::WriteOnly) )
    {
        QString dt = "mylovedandanxiaohai";
        double value = 3.14;

        file.write(dt.toStdString().c_str());
        file.write(reinterpret_cast<char*>(&value), sizeof(value));

        file.close();
    }

    if( file.open(QIODevice::ReadOnly) )
    {
        QString dt = "";
        double value = 0;

        dt = QString(file.read(19));
        file.read(reinterpret_cast<char*>(&value), sizeof(value));

        file.close();

        qDebug() << dt;
        qDebug() << value;
    }

    return a.exec();
}

Run Results

2. Buffer and directory operations

The concept of buffer in A.Qt
1. Buffers are essentially a continuous storage space
2.QBuffer is a buffer-related class in Qt
3. Buffers can be viewed as a special IO device in Qt
4. File stream auxiliary classes can be used directly to manipulate buffers
How to use QBuffer buffer
Code Samples

#include <QtCore/QCoreApplication>
#include <QBuffer>
#include <QByteArray>
#include <QDataStream>
#include <QDebug>

void write_buffer(int type, QBuffer& buffer)
{
    if( buffer.open(QIODevice::WriteOnly) )
    {
        QDataStream out(&buffer);

        out << type;

        if( type == 0 )
        {
            out << QString("mylove");
            out << QString("3.1415");
        }
        else if( type == 1 )
        {
            out << 3;
            out << 1415;
        }
        else if( type == 2 )
        {
            out << 3.1415;
        }

        buffer.close();
    }
}

void read_buffer(QBuffer& buffer)
{
    if( buffer.open(QIODevice::ReadOnly) )
    {
        int type = -1;
        QDataStream in(&buffer);

        in >> type;

        if( type == 0 )
        {
            QString dt = "";
            QString pi = "";

            in >> dt;
            in >> pi;

            qDebug() << dt;
            qDebug() << pi;
        }
        else if( type == 1 )
        {
            int a = 0;
            int b = 0;

            in >> a;
            in >> b;

            qDebug() << a;
            qDebug() << b;
        }
        else if( type == 2 )
        {
            double pi = 0;

            in >> pi;

            qDebug() << pi;
        }

        buffer.close();
    }
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QByteArray array;
    QBuffer buffer(&array);

    write_buffer(0, buffer);
    read_buffer(buffer);

    return a.exec();
}

Run Results

Use of B.QBuffer Buffer
1. Different types of data transfer between threads
2. Caching data returns from external devices
3. Data read speed is less than data write speed
C.QDir is a powerful directory operation class in Qt
1. Unified use of'/'for directory separators in Qt
2.QDir can do anything with directories--create, delete, rename
3.QDir is able to get all entries in the specified directory--files and folders
4.QDir is able to use filter strings to get specified entries
5.QDir can get all the root directories in the system
Catalog operations basic example

C.QFileSystemWatcher monitors state changes of files and directories
1. Ability to monitor the status of specific directories and files
2. Ability to monitor multiple directories and files at the same time
3. A signal will be triggered when a directory or file changes
4. Signals can be captured and responded to by the mechanism of signal and slot

Tags: C++ Qt less

Posted on Tue, 13 Aug 2019 19:20:45 -0700 by illushinz