QIODevice 类学习笔记
Isaaccwoo 2015年12月10日
一、 简介
QIODevice用于对输入输出设备进行管理。输入设备有两种类型,一种是随机访问设备(Random-accessdevices),如文件、缓冲区等;另一种是时序设备(Sequential device),如网络、进程等。可以通过isSequential()函数分辨设备是哪种类型的。
二、 自定义类型
设备的打开方式:OpenMode
enum QIODevice::OpenModeFlag
flags QIODevice::OpenMode
Constant |
Value |
Description |
QIODevice::NotOpen |
0x0000 |
The device is not open. |
QIODevice::ReadOnly |
0x0001 |
The device is open for reading. |
QIODevice::WriteOnly |
0x0002 |
The device is open for writing. Note that this mode implies Truncate. |
QIODevice::ReadWrite |
ReadOnly | WriteOnly |
The device is open for reading and writing. |
QIODevice::Append |
0x0004 |
The device is opened in append mode so that all data is written to the end of the file. |
QIODevice::Truncate |
0x0008 |
If possible, the device is truncated before it is opened. All earlier contents of the device are lost. |
QIODevice::Text |
0x0010 |
When reading, the end-of-line terminators are translated to '\n'. When writing, the end-of-line terminators are translated to the local encoding, for example '\r\n' for Win32. |
QIODevice::Unbuffered |
0x0020 |
Any buffer in the device is bypassed. |
三、 正确的打开方式 输入输出设备的使用方法
1. 构造与析构
函数 |
说明 |
QIODevice() |
|
QIODevice(QObject * parent) |
|
~QIODevice() |
继承类需自行保证close() |
2. OpenMode
返回类型 |
函数 |
说明 |
OpenMode |
openMode() const |
QIODevice::OpenMode |
void [protected] |
setOpenMode(OpenMode openMode) |
|
bool |
isOpen() const |
QIODevice::NotOpen |
bool |
isReadable() const |
QIODevice::ReadOnly |
bool |
isWritable() const |
QIODevice::WriteOnly |
bool |
isTextModeEnabled() const |
QIODevice::Text |
void |
setTextModeEnabled(bool enabled) |
QIODevice::Text |
bool [virtual] |
isSequential() const |
|
3. 打开关闭设备
返回类型 |
函数 |
说明 |
bool [virtual] |
open(OpenMode mode) |
|
bool |
isOpen() const |
|
void [virtual] |
close() |
|
void [signal] |
aboutToClose() |
|
Bool [virtual] |
reset() |
重打开设备。 不适用于QIODevice::Text类设备。 |
4. 数据读写操作及控制
输入函数 | ||
返回类型 |
函数 |
说明 |
bool [virtual] |
waitForReadyRead(int msecs) |
等待可读 |
void [signal] |
readyRead() |
中断式可读 |
qint64 [virtual] |
bytesAvailable() const |
查询式可读 |
qint64 [protected] [pure virtual] |
readData(char * data, qint64 maxSize) |
读函数 |
qint64 |
read(char * data, qint64 maxSize) |
读 |
QByteArray |
read(qint64 maxSize) |
. |
qint64 |
peek(char * data, qint64 maxSize) |
“偷看” |
QByteArray |
peek(qint64 maxSize) |
. |
QByteArray |
readAll() |
读全部 |
bool [virtual] |
canReadLine() const |
可读行 |
qint64 [virtual] [protected] |
readLineData(char * data, qint64 maxSize) |
读行函数 |
qint64 |
readLine(char * data, qint64 maxSize) |
读行 (maxSize包括行结束字符) |
QByteArray |
readLine(qint64 maxSize = 0) |
读行 |
bool |
getChar(char * c) |
读char |
void |
ungetChar(char c) |
undo char |
void [signal] |
readChannelFinished() |
输入通道结束。 但此时可能仍有数据可读。 |
输出函数 | ||
返回类型 |
函数 |
说明 |
bool [virtual] |
waitForBytesWritten(int msecs) |
等待已写 |
void [signal] |
bytesWritten(qint64 bytes) |
中断式已写 |
qint64 [virtual] |
bytesToWrite() const |
查询式写完 |
qint64 [protected] [pure virtual] |
writeData(const char * data, qint64 maxSize) |
写函数 |
qint64 |
write(const char * data, qint64 maxSize) |
写 |
qint64 |
write(const char * data) |
. |
qint64 |
write(const QByteArray & byteArray) |
. |
bool |
putChar(char c) |
写char |
isSequential()不同有区别的函数 | ||
返回类型 |
函数 |
说明 |
qint64 [virtual] |
pos() const |
|
bool [virtual] |
seek(qint64 pos) |
|
qint64 [virtual] |
size() const |
随机访问设备为区域大小,时序设备为bytesAvailable() |
其他函数 | ||
返回类型 |
函数 |
说明 |
bool [virtual] |
atEnd() const |
|
QString |
errorString() const |
|
void [protected] |
setErrorString(const QString & str) |
|
注:qint64返回值一般原则为,错误返回-1,成功返回>=0