QT中缓冲区- QBuffer

来源:互联网 发布:销售智慧软件 编辑:程序博客网 时间:2024/06/03 16:00

QBuffer 缓冲区的使用方式

QBuffer 缓冲区的使用场合:
1.在线程间进行不同类型不同数量的数据传递

2.缓存外部设备中的数据访问

3.数据读取速度小于数据写入速度

写缓冲区:

   QByteArray array;
    QBuffer buffer(&array);
int  type =0;

 if( buffer.open(QIODevice::WriteOnly) )
    {
        QDataStream out(&buffer);
        out << type;
        if( type == 0 )
        {
            out << QString("hello world");
            out << QString("3.1415");
        }
        else if( type == 1 )
        {
            out << 3;
            out << 1415;
        }
        else if( type == 2 )
        {
            out << 3.1415;
        }
        buffer.close();
    }
}
读缓冲区:
  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();
    }

阅读全文
0 0