QBuffer可以在内存中开辟一块空间用来存储临时数据,QBuffer继承自QIODevice。 这个比较好理解,直接看代码吧(记得包含QBuffer头文件):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 QBuffer membuf; if (membuf.open (QIODevice::WriteOnly)){ membuf.write ("Hello" ); membuf.write (" This is QBuffer" ); membuf.close (); membuf.open (QIODevice::ReadOnly); cout << membuf.readAll ().toStdString () << endl; membuf.close (); }
输出内容如下:
1 2 3 11:35:49: Starting /home/fire/codeSet/QtSet/build-untitled9-unknown-Debug/untitled9 ... Hello This is QBuffer 11:35:52: /home/fire/codeSet/QtSet/build-untitled9-unknown-Debug/untitled9 exited with code 0
你也可以这么做:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 QByteArray arr; QBuffer membuf (&arr) ;QDataStream stream; if (membuf.open (QIODevice::WriteOnly)){ QString msg ("Now!Buf!" ) ; stream.setDevice (&membuf); stream << msg << 777 ; membuf.close (); QString resq; int resi; membuf.open (QIODevice::ReadOnly); stream >> resq >> resi; std::cout << resq.toUtf8 ().data () << resi << endl; }
输出如下:
1 2 3 11:41:50: Starting /home/fire/codeSet/QtSet/build-untitled9-unknown-Debug/untitled9 ... Now!Buf!777 11:41:54: /home/fire/codeSet/QtSet/build-untitled9-unknown-Debug/untitled9 exited with code 0
这篇文章就介绍到这里,本篇完。