QTextStream和QDataStream都是面向資料流的,都適用於QIODevice,但是他們的側重點不同。可以這麼說,QTextStream能做的事情QDataStream都能做。QTextStream 側重於向QIODevice文字讀寫,這裡所說的文字指的是普通的簡單的QChar,QString,QLatin1Char,int等等之內的,和C語言中寫檔案或者網路傳輸的時候,先將內容填充到一個buffer,進行操作有點類似,屬於普通的輕量級的流操作類。view plaincopy to clipboardprint?1. //下面程式碼就是寫檔案操作2. QFile data("output.txt");3. if (data.open(QFile::WriteOnly | QFile::Truncate)) {4. QTextStream out(&data);5. out > tmp//此時tmp的座標就是tmp(8, 6); QPoint pos(8, 6) QPoint tmp; QByteArray datagram; QDataStream out(&datagram, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_3); out > tmp//此時tmp的座標就是tmp(8, 6);QDataStream是根據型別以及輸出輸入順序來決定輸出資料。列舉下面的例子來說明問題view plaincopy to clipboardprint?1. QFile file("file.dat");2. file.open(QIODevice::WriteOnly);3. QDataStream out(&file); // we will serialize the data into the file4. out > str >> a; // extract "the answer is" and 42
QTextStream和QDataStream都是面向資料流的,都適用於QIODevice,但是他們的側重點不同。可以這麼說,QTextStream能做的事情QDataStream都能做。QTextStream 側重於向QIODevice文字讀寫,這裡所說的文字指的是普通的簡單的QChar,QString,QLatin1Char,int等等之內的,和C語言中寫檔案或者網路傳輸的時候,先將內容填充到一個buffer,進行操作有點類似,屬於普通的輕量級的流操作類。view plaincopy to clipboardprint?1. //下面程式碼就是寫檔案操作2. QFile data("output.txt");3. if (data.open(QFile::WriteOnly | QFile::Truncate)) {4. QTextStream out(&data);5. out > tmp//此時tmp的座標就是tmp(8, 6); QPoint pos(8, 6) QPoint tmp; QByteArray datagram; QDataStream out(&datagram, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_3); out > tmp//此時tmp的座標就是tmp(8, 6);QDataStream是根據型別以及輸出輸入順序來決定輸出資料。列舉下面的例子來說明問題view plaincopy to clipboardprint?1. QFile file("file.dat");2. file.open(QIODevice::WriteOnly);3. QDataStream out(&file); // we will serialize the data into the file4. out > str >> a; // extract "the answer is" and 42