Qt-- text and data flow, data manipulation and operation of the buffer

I. text flow and data flow

A.Qt-speaking into two categories file type
1. Text File - file content is readable text characters
2. Data File - file content is direct binary data
QFile directly supports read and write data files and text files
Qt-- text and data flow, data manipulation and operation of the buffer
B.Qt helper classes is provided to simplify the text file / data file read and write
all data 1.QTextStream-- written into readable text
2.QDataStream-- write data into binary data according to the type of
IO device auxiliary class use
Qt-- text and data flow, data manipulation and operation of the buffer
different versions of Qt stream file formats may be different
void setVersion (int v) // set the version number
int version () const // Get the version number read

sample Code

#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();
}

operation result
Qt-- text and data flow, data manipulation and operation of the buffer

II. Buffer operations and directory operations

A.Qt buffer concept in
nature 1. The buffer is a contiguous storage space
2.QBuffer Qt is related classes in the buffer
3. In Qt buffer may be viewed as a special IO device
4. class file stream can be directly used to operate the auxiliary buffer
QBuffer buffer usage Qt-- text and data flow, data manipulation and operation of the buffer
code sample

#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();
}

Operating results
Qt-- text and data flow, data manipulation and operation of the buffer
B.QBuffer buffer usage scenarios
1. Different types of data transfer between threads in
the data buffer 2. The external device returns
3. The data read speed less than the data write speed
C.QDir powerful functions Qt directory manipulation classes
1.Qt in unified directory separator use '/'
2.QDir can be any directory operations - create, delete, rename
3.QDir be able to get all of the entries in the specified directory - files and folders clip
4.QDir possible to use a filter string from the designated entry
of all the root directory system can acquire 5.QDir
directory exemplary basic operation
Qt-- text and data flow, data manipulation and operation of the buffer

C.QFileSystemWatcher for monitoring the state of files and directories change
1 can monitor the state of the specific directory and file
2 can simultaneously monitor a plurality of files and directories
3. When the directory or file is changed a trigger signal
4 by mechanism groove signal capture signal and in responseQt-- text and data flow, data manipulation and operation of the buffer

Guess you like

Origin blog.51cto.com/13475106/2429347