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();}
本文详细介绍了QBuffer缓冲区的使用方式,包括在线程间传递不同类型的数据、缓存外部设备数据及处理读写速度不匹配的情况。通过具体示例展示了如何创建缓冲区并进行数据的写入和读取。
958

被折叠的 条评论
为什么被折叠?



