• 4660阅读
  • 1回复

关于QIODevice::bytesAvailable() 函数 [复制链接]

上一主题 下一主题
离线wuleeemail
 

只看楼主 倒序阅读 楼主  发表于: 2015-06-18
QIODevice::bytesAvailable()是这么说的:Returns the number of bytes
that are available for reading. This function is commonly used with sequential
devices to determine the number of bytes to allocate in a buffer beforereading.
有一个Qt的例子是这样的:

uint time2u = 0;
QDataStream in(tcpSocket);
in.setVersion(QDataStream::Qt_4_3);
if(time2u==0)
   {
if(tcpSocket->bytesAvailable()<(int)sizeof(uint))  return;
  in>>time2u;
}
dateTimeEdit->setDateTime(QDateTime::fromTime_t(time2u));
getBtn->setEnabled(true);
}我想问的是上面代码里if(tcpSocket->bytesAvailable()<(int)sizeof(uint))是什么意思?
按理说tcpSocket->bytesAvailable()要比较应该是读到的字节数,怎么会是(int)sizeof(uint)呢?
离线johnyork

只看该作者 1楼 发表于: 2015-06-18
一次读4个字节输出给time2u,不足4个字节就不读,避免自己组织数据的麻烦。当然你要不嫌麻烦你也可以全部读出来自己移位。
快速回复
限100 字节
 
上一个 下一个