QIODevice

QIODevice 

#include <QIODevice>

Public Functions

  QIODevice()
  QIODevice(QObject *parent)
virtual ~QIODevice()
virtual bool atEnd() const
virtual qint64 bytesAvailable() const
virtual qint64 bytesToWrite() const
virtual bool canReadLine() const
virtual void close()
void commitTransaction()
int currentReadChannel() const
int currentWriteChannel() const
QString errorString() const
bool getChar(char *c)
bool isOpen() const
bool isReadable() const
virtual bool isSequential() const
bool isTextModeEnabled() const
bool isTransactionStarted() const
bool isWritable() const
virtual bool open(QIODevice::OpenMode mode)
QIODevice::OpenMode openMode() const
qint64 peek(char *data, qint64 maxSize)
QByteArray peek(qint64 maxSize)
virtual qint64 pos() const
bool putChar(char c)
qint64 read(char *data, qint64 maxSize)
QByteArray read(qint64 maxSize)
QByteArray readAll()
int readChannelCount() const
qint64 readLine(char *data, qint64 maxSize)
QByteArray readLine(qint64 maxSize = 0)
virtual bool reset()
void rollbackTransaction()
virtual bool seek(qint64 pos)
void setCurrentReadChannel(int channel)
void setCurrentWriteChannel(int channel)
void setTextModeEnabled(bool enabled)
virtual qint64 size() const
qint64 skip(qint64 maxSize)
void startTransaction()
void ungetChar(char c)
virtual bool waitForBytesWritten(int msecs)
virtual bool waitForReadyRead(int msecs)
qint64 write(const char *data, qint64 maxSize)
qint64 write(const char *data)
qint64 write(const QByteArray &byteArray)
int writeChannelCount() const
  • 32 public functions inherited from QObject

Signals

void aboutToClose()
void bytesWritten(qint64 bytes)
void channelBytesWritten(int channel, qint64 bytes)
void channelReadyRead(int channel)
void readChannelFinished()
void readyRead()
  • 2 signals inherited from QObject

Protected Functions

virtual qint64 readData(char *data, qint64 maxSize) = 0
virtual qint64 readLineData(char *data, qint64 maxSize)
void setErrorString(const QString &str)
void setOpenMode(QIODevice::OpenMode openMode)
virtual qint64 writeData(const char *data, qint64 maxSize) = 0
  • 9 protected functions inherited from QObject

Additional Inherited Members

  • 1 property inherited from QObject
  • 1 public slot inherited from QObject
  • 11 static public members inherited from QObject

 

 

详细说明

QIODevice类是Qt中所有I/O设备的基本接口类。

 

QIODevices为支持读写数据块的设备(如QFile、QBuffer和QTcpSocket)提供了通用实现和抽象接口。QIODevices是抽象的,不能实例化,但通常使用它定义的接口来提供与设备无关的I/O功能。例如,Qt的XML类在QIODevices指针上操作,允许它们与各种设备(如文件和缓冲区)一起使用。

 

在访问设备之前,必须调用open()来设置正确的OpenMode(如ReadOnly或ReadWrite)。然后,您可以使用write()或putChar()对设备进行写入,并通过调用read()、readLine()或readAll()进行读取。使用完设备后,请调用close()。

 

QIODevices区分两种类型的设备:随机访问设备和顺序设备。

 

随机访问设备支持使用seek()查找任意位置。通过调用pos()可以获得文件中的当前位置。QFile和QBuffer是随机接入设备的示例。

顺序设备不支持查找任意位置。数据必须一次性读取。函数pos()和size()不适用于顺序设备。QTcpSocket和QProcess是顺序设备的示例。

您可以使用isSequencel()来确定设备的类型。

 

当有新数据可供读取时,QIODevices会发出readyRead();例如,如果新的数据已经到达网络,或者如果额外的数据被附加到您正在读取的文件中。您可以调用bytesAvailable()来确定当前可供读取的字节数。在使用异步设备(如QTcpSocket)编程时,通常将bytesAvailable()与readyRead()信号一起使用,其中数据片段可以在任意时间点到达。每次向设备写入有效负载数据时,QIODevice都会发出bytesWritten()信号。使用bytesToWrite()来确定当前等待写入的数据量。

 

QIODevices的某些子类,如QTcpSocket和QProcess,是异步的。这意味着诸如write()或read()之类的I/O函数总是立即返回,而当控制返回到事件循环时,可能会发生与设备本身的通信。QIODevices提供的函数允许您强制立即执行这些操作,同时阻塞调用线程,而不进入事件循环。这允许QIODevicee子类在没有事件循环的情况下使用,或者在单独的线程中使用:

 

waitForReadyRead()-此函数将挂起调用线程中的操作,直到有新数据可供读取为止。

waitForBytesWritten()-此函数将挂起调用线程中的操作,直到将一个有效负载的数据写入设备为止。

等待。。。。()-QIODevices的子类实现设备特定操作的阻塞功能。例如,QProcess有一个名为waitForStarted()的函数,它会挂起调用线程中的操作,直到进程启动为止。

从主GUI线程调用这些函数可能会导致用户界面冻结。示例:

 

  QProcess gzip;
  gzip.start("gzip", QStringList() << "-c");
  if (!gzip.waitForStarted())
      return false;

  gzip.write("uncompressed data");

  QByteArray compressed;
  while (gzip.waitForReadyRead())
      compressed += gzip.readAll();

 

 

通过子类化QIODevices,您可以为自己的I/O设备提供相同的接口。QIODevices的子类仅用于实现受保护的readData()和writeData()函数。QIODevice使用这些函数来实现其所有的便利函数,如getChar()、readLine()和write()。QIODevices还为您处理访问控制,因此如果调用writeData(),您可以安全地假设设备是以写模式打开的。

 

一些子类,如QFile和QTcpSocket,是使用用于数据中间存储的内存缓冲区来实现的。这减少了所需的设备访问呼叫的数量,而这些呼叫通常非常缓慢。缓冲可以使getChar()和putChar()等函数快速运行,因为它们可以在内存缓冲区上操作,而不是直接在设备本身上操作。但是,某些I/O操作在使用缓冲区时不能很好地工作。例如,如果几个用户打开同一个设备并逐个字符地读取它,那么当他们打算分别读取一个单独的块时,他们最终可能会读取相同的数据。因此,QIODevice允许您通过将Unbuffered标志传递给open()来绕过任何缓冲。在对QIODevices进行子类化时,请记住在设备以Unbuffered模式打开时绕过可能使用的任何缓冲区。

 

通常,来自异步设备的传入数据流是分段的,数据块可以在任意时间点到达。要处理数据结构的不完整读取,请使用QIODevices实现的事务机制。有关更多详细信息,请参阅startTransaction()和相关函数。

 

一些顺序设备支持通过多个通道进行通信。这些通道表示具有独立排序传递特性的独立数据流。打开设备后,您可以通过调用readChannelCount()和writeChannelCount()函数来确定通道的数量。要在通道之间切换,请分别调用setCurrentReadChannel()和setCurrentWriteChannel(()。QIODevices还提供额外的信号来处理每个信道的异步通信。

 

 

ConstantValueDescription
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.
QIODevice::NewOnly 0x0040 Fail if the file to be opened already exists. Create and open the file only if it does not exist. There is a guarantee from the operating system that you are the only one creating and opening the file. Note that this mode implies WriteOnly, and combining it with ReadWrite is allowed. This flag currently only affects QFile. Other classes might use this flag in the future, but until then using this flag with any classes other than QFile may result in undefined behavior.
QIODevice::ExistingOnly 0x0080 Fail if the file to be opened does not exist. This flag must be specified alongside ReadOnly, WriteOnly, or ReadWrite. Note that using this flag with ReadOnly alone is redundant, as ReadOnly already fails when the file does not exist. This flag currently only affects QFile. Other classes might use this flag in the future, but until then using this flag with any classes other than QFile may result in undefined behavior.

 

 

####################

posted @ 2023-07-14 19:19  西北逍遥  阅读(41)  评论(0编辑  收藏  举报