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