• PySide 模块
  • PySide.QtCore
  • 内容表

    上一话题

    QThread

    下一话题

    QBuffer

    QIODevice

    继承者: QNetworkReply , QBuffer , QAbstractSocket , QUdpSocket , QLocalSocket , QFile , QTemporaryFile , QProcess , QTcpSocket , QSslSocket

    概要

    函数

    虚函数

    详细描述

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

    PySide.QtCore.QIODevice provides both a common implementation and an abstract interface for devices that support reading and writing of blocks of data, such as PySide.QtCore.QFile , PySide.QtCore.QBuffer and PySide.QtNetwork.QTcpSocket . PySide.QtCore.QIODevice is abstract and can not be instantiated, but it is common to use the interface it defines to provide device-independent I/O features. For example, Qt's XML classes operate on a PySide.QtCore.QIODevice pointer, allowing them to be used with various devices (such as files and buffers).

    在访问设备之前, PySide.QtCore.QIODevice.open() must be called to set the correct OpenMode (譬如 ReadOnly or ReadWrite )。然后,可以写入设备采用 PySide.QtCore.QIODevice.write() or PySide.QtCore.QIODevice.putChar() , and read by calling either PySide.QtCore.QIODevice.read() , PySide.QtCore.QIODevice.readLine() ,或 PySide.QtCore.QIODevice.readAll() 。调用 PySide.QtCore.QIODevice.close() when you are done with the device.

    PySide.QtCore.QIODevice distinguishes between two types of devices: random-access devices and sequential devices.

    可以使用 PySide.QtCore.QIODevice.isSequential() to determine the type of device.

    PySide.QtCore.QIODevice 发射 PySide.QtCore.QIODevice.readyRead() when new data is available for reading; for example, if new data has arrived on the network or if additional data is appended to a file that you are reading from. You can call PySide.QtCore.QIODevice.bytesAvailable() to determine the number of bytes that are currently available for reading. It's common to use PySide.QtCore.QIODevice.bytesAvailable() together with the PySide.QtCore.QIODevice.readyRead() signal when programming with asynchronous devices such as PySide.QtNetwork.QTcpSocket , where fragments of data can arrive at arbitrary points in time. PySide.QtCore.QIODevice 发射 PySide.QtCore.QIODevice.bytesWritten() signal every time a payload of data has been written to the device. Use PySide.QtCore.QIODevice.bytesToWrite() to determine the current amount of data waiting to be written.

    某些子类化的 PySide.QtCore.QIODevice ,譬如 PySide.QtNetwork.QTcpSocket and PySide.QtCore.QProcess , 是异步的。这意味着 I/O 函数,譬如 PySide.QtCore.QIODevice.write() or PySide.QtCore.QIODevice.read() always return immediately, while communication with the device itself may happen when control goes back to the event loop. PySide.QtCore.QIODevice provides functions that allow you to force these operations to be performed immediately, while blocking the calling thread and without entering the event loop. This allows PySide.QtCore.QIODevice subclasses to be used without an event loop, or in a separate thread:

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

    gzip = QProcess()
    gzip.start("gzip", ["-c"])
    if not gzip.waitForStarted():
        return False
    gzip.write("uncompressed data")
    compressed = QByteArray()
    while gzip.waitForReadyRead():
        compressed += gzip.readAll()
    										

    By subclassing PySide.QtCore.QIODevice , you can provide the same interface to your own I/O devices. Subclasses of PySide.QtCore.QIODevice are only required to implement the protected PySide.QtCore.QIODevice.readData() and PySide.QtCore.QIODevice.writeData() 函数。 PySide.QtCore.QIODevice uses these functions to implement all its convenience functions, such as PySide.QtCore.QIODevice.getChar() , PySide.QtCore.QIODevice.readLine() and PySide.QtCore.QIODevice.write() . PySide.QtCore.QIODevice also handles access control for you, so you can safely assume that the device is opened in write mode if PySide.QtCore.QIODevice.writeData() 被调用。

    某些子类,譬如 PySide.QtCore.QFile and PySide.QtNetwork.QTcpSocket ,使用内存缓冲实现数据的中间体存储。这减少了要求设备的访问调用 (经常非常慢) 数。缓冲使函数像 PySide.QtCore.QIODevice.getChar() and PySide.QtCore.QIODevice.putChar() fast, as they can operate on the memory buffer instead of directly on the device itself. Certain I/O operations, however, don't work well with a buffer. For example, if several users open the same device and read it character by character, they may end up reading the same data when they meant to read a separate chunk each. For this reason, PySide.QtCore.QIODevice allows you to bypass any buffering by passing the Unbuffered flag to PySide.QtCore.QIODevice.open() . When subclassing PySide.QtCore.QIODevice , remember to bypass any buffer you may use when the device is open in Unbuffered mode.

    class PySide.QtCore. QIODevice
    class PySide.QtCore. QIODevice ( parent )
    参数: parent PySide.QtCore.QObject

    构造 PySide.QtCore.QIODevice 对象。

    构造 PySide.QtCore.QIODevice 对象采用给定 parent .

    PySide.QtCore.QIODevice. OpenModeFlag

    此枚举用于 PySide.QtCore.QIODevice.open() to describe the mode in which a device is opened. It is also returned by PySide.QtCore.QIODevice.openMode() .

    常量 描述
    QIODevice.NotOpen 设备未打开。
    QIODevice.ReadOnly 打开设备以供读取。
    QIODevice.WriteOnly The device is open for writing.
    QIODevice.ReadWrite 设备打开为读取和写入。
    QIODevice.Append The device is opened in append mode, so that all data is written to the end of the file.
    QIODevice.Truncate 若可能,会截取设备在它被打开之前。设备的所有早期内容将丢失。
    QIODevice.Text 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 ‘rn' for Win32.
    QIODevice.Unbuffered 绕过任何设备缓冲。

    某些标志,如 Unbuffered and Truncate 没有意义,当用于某些子类时。其中某些限定由子类表示的设备类型所隐含。在其它情况下,限定可能是由于实现,或可能是通过底层平台强加;例如, PySide.QtNetwork.QTcpSocket 不支持 Unbuffered 模式,且局限在本机 API 以阻止 PySide.QtCore.QFile 从支持 Unbuffered 在 Windows。

    PySide.QtCore.QIODevice. aboutToClose ( )
    PySide.QtCore.QIODevice. atEnd ( )
    返回类型: PySide.QtCore.bool

    Returns true if the current read and write position is at the end of the device (i.e. there is no more data available for reading on the device); otherwise returns false.

    For some devices, PySide.QtCore.QIODevice.atEnd() can return true even though there is more data to read. This special case only applies to devices that generate data in direct response to you calling PySide.QtCore.QIODevice.read() (如, /dev or /proc files on Unix and Mac OS X, or console input / stdin 在所有平台)。

    PySide.QtCore.QIODevice. bytesAvailable ( )
    返回类型: PySide.QtCore.qint64

    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 before reading.

    Subclasses that reimplement this function must call the base implementation in order to include the size of QIODevices' buffer. Example:

    def bytesAvailable(self):
        return buffer.size() + QIODevice.bytesAvailable()
    											
    PySide.QtCore.QIODevice. bytesToWrite ( )
    返回类型: PySide.QtCore.qint64

    For buffered devices, this function returns the number of bytes waiting to be written. For devices with no buffer, this function returns 0.

    PySide.QtCore.QIODevice. bytesWritten ( bytes )
    参数: bytes PySide.QtCore.qint64
    PySide.QtCore.QIODevice. canReadLine ( )
    返回类型: PySide.QtCore.bool

    Returns true if a complete line of data can be read from the device; otherwise returns false.

    注意:没有办法确定是否可以读取的无缓冲设备始终返回 false。

    此函数经常被调用结合 PySide.QtCore.QIODevice.readyRead() 信号。

    重实现此函数的子类必须调用基实现以便包括内容为 PySide.QtCore.QIODevice ‘s buffer. Example:

    def canReadLine(self):
        return buffer.contains('\n') or QIODevice.canReadLine()
    											
    PySide.QtCore.QIODevice. close ( )

    首先发射 PySide.QtCore.QIODevice.aboutToClose() , then closes the device and sets its OpenMode to NotOpen 。错误字符串也会被重置。

    PySide.QtCore.QIODevice. errorString ( )
    返回类型: unicode

    返回人类可读的最后发生的设备错误描述。

    PySide.QtCore.QIODevice. getChar ( )
    返回类型: PySide.QtCore.bool

    从设备读取 1 字符并把它存储在 c 。若 c is 0, the character is discarded. Returns true on success; otherwise returns false.

    PySide.QtCore.QIODevice. isOpen ( )
    返回类型: PySide.QtCore.bool

    Returns true if the device is open; otherwise returns false. A device is open if it can be read from and/or written to. By default, this function returns false if PySide.QtCore.QIODevice.openMode() 返回 NotOpen .

    PySide.QtCore.QIODevice. isReadable ( )
    返回类型: PySide.QtCore.bool

    Returns true if data can be read from the device; otherwise returns false. Use PySide.QtCore.QIODevice.bytesAvailable() to determine how many bytes can be read.

    这是方便的校验函数若 OpenMode 的设备包含 ReadOnly 标志。

    PySide.QtCore.QIODevice. isSequential ( )
    返回类型: PySide.QtCore.bool

    Returns true if this device is sequential; otherwise returns false.

    Sequential devices, as opposed to a random-access devices, have no concept of a start, an end, a size, or a current position, and they do not support seeking. You can only read from the device when it reports that data is available. The most common example of a sequential device is a network socket. On Unix, special files such as /dev/zero and fifo pipes are sequential.

    另一方面,常规文件确实支持随机访问。它们拥有大小和当前位置,且它们还支持在数据流中向后和向前寻址。常规文件是非顺序的。

    PySide.QtCore.QIODevice 实现返回 false。

    PySide.QtCore.QIODevice. isTextModeEnabled ( )
    返回类型: PySide.QtCore.bool

    返回 true 若 文本 flag is enabled; otherwise returns false.

    PySide.QtCore.QIODevice. isWritable ( )
    返回类型: PySide.QtCore.bool

    Returns true if data can be written to the device; otherwise returns false.

    这是方便的校验函数若 OpenMode 的设备包含 WriteOnly 标志。

    PySide.QtCore.QIODevice. open ( mode )
    参数: mode PySide.QtCore.QIODevice.OpenMode
    返回类型: PySide.QtCore.bool
    PySide.QtCore.QIODevice. openMode ( )
    返回类型: PySide.QtCore.QIODevice.OpenMode

    返回设备被打开的模式;即: ReadOnly or WriteOnly .

    PySide.QtCore.QIODevice. peek ( maxlen )
    参数: maxlen PySide.QtCore.qint64
    返回类型: PySide.QtCore.QByteArray

    这是重载函数。

    窥视最多 maxSize 字节从设备,返回窥视数据作为 PySide.QtCore.QByteArray .

    范例:

    def isExeFile(file_):
        return file_.peek(2) == "MZ"
    											

    此函数没有办法报告错误;返回空 QByteArray() 可能意味着当前没有数据可供窥视,或发生错误。

    PySide.QtCore.QIODevice. pos ( )
    返回类型: PySide.QtCore.qint64

    For random-access devices, this function returns the position that data is written to or read from. For sequential devices or closed devices, where there is no concept of a “current position”, 0 is returned.

    设备的当前读/写位置的内部维护由 PySide.QtCore.QIODevice ,因此重实现此函数不必要。当子类化 PySide.QtCore.QIODevice ,使用 QIODevice.seek() to notify PySide.QtCore.QIODevice 关于设备位置的变化。

    PySide.QtCore.QIODevice. putChar ( c )
    参数: c PySide.QtCore.char
    返回类型: PySide.QtCore.bool

    写入字符 c to the device. Returns true on success; otherwise returns false.

    PySide.QtCore.QIODevice. read ( maxlen )
    参数: maxlen PySide.QtCore.qint64
    返回类型: PySide.QtCore.QByteArray

    这是重载函数。

    读取最多 maxSize 字节从设备,并把读取数据返回作为 PySide.QtCore.QByteArray .

    此函数没有办法报告错误;返回空 QByteArray() 可以意味着目前没有数据可供读取,或发生错误。

    PySide.QtCore.QIODevice. readAll ( )
    返回类型: PySide.QtCore.QByteArray

    这是重载函数。

    Reads all available data from the device, and returns it as a PySide.QtCore.QByteArray .

    此函数没有办法报告错误;返回空 QByteArray() 可以意味着目前没有数据可供读取,或发生错误。

    PySide.QtCore.QIODevice. readChannelFinished ( )
    PySide.QtCore.QIODevice. readData ( maxlen )
    参数: maxlen PySide.QtCore.qint64
    返回类型: PyObject

    读取到 maxSize 字节从设备到 data ,并返回读取字节数;返回 -1 若发生错误。

    若没有字节要读取且从不会有更多字节可用 (范例包括:套接字被关闭、管道被关闭、子进程已完成),此函数返回 -1。

    此函数被调用由 PySide.QtCore.QIODevice 。重实现此函数,当创建子类为 PySide.QtCore.QIODevice .

    当重实现此函数时,此函数在返回之前读取所有要求数据是很重要的。这是必需的为 PySide.QtCore.QDataStream 能够在类中运转。 PySide.QtCore.QDataStream 假定所有请求信息被读取,因此,不会重试读取若存在问题。

    PySide.QtCore.QIODevice. readLine ( [ maxlen=0 ] )
    参数: maxlen PySide.QtCore.qint64
    返回类型: PySide.QtCore.QByteArray

    这是重载函数。

    从设备读取行,但不超过 maxSize characters, and returns the result as a PySide.QtCore.QByteArray .

    此函数没有办法报告错误;返回空 QByteArray() 可以意味着目前没有数据可供读取,或发生错误。

    PySide.QtCore.QIODevice. readLineData ( maxlen )
    参数: maxlen PySide.QtCore.qint64
    返回类型: PyObject

    读取到 maxSize 字符到 data 并返回读取的字符数。

    此函数被调用由 PySide.QtCore.QIODevice.readLine() , and provides its base implementation, using PySide.QtCore.QIODevice.getChar() . Buffered devices can improve the performance of PySide.QtCore.QIODevice.readLine() by reimplementing this function.

    PySide.QtCore.QIODevice.readLine() appends a ‘0' byte to data ; PySide.QtCore.QIODevice.readLineData() does not need to do this.

    若重实现此函数,小心返回正确值:它应返回在这一行中读取的字节数 (包括终止换行符),或 0 若此时没有行要读取。若发生错误,它应返回 -1,当且仅当没有字节要读取时。读取超过 EOF 认为出错。

    PySide.QtCore.QIODevice. readyRead ( )
    PySide.QtCore.QIODevice. reset ( )
    返回类型: PySide.QtCore.bool

    Seeks to the start of input for random-access devices. Returns true on success; otherwise returns false (for example, if the device is not open).

    注意,当使用 PySide.QtCore.QTextStream PySide.QtCore.QFile ,调用 PySide.QtCore.QIODevice.reset() PySide.QtCore.QFile 将不会有预期结果,因为 PySide.QtCore.QTextStream 缓冲文件。使用 QTextStream.seek() function instead.

    PySide.QtCore.QIODevice. seek ( pos )
    参数: pos PySide.QtCore.qint64
    返回类型: PySide.QtCore.bool

    对于随机访问设备,此函数将当前位置设为 pos , returning true on success, or false if an error occurred. For sequential devices, the default behavior is to do nothing and return false.

    当子类化 PySide.QtCore.QIODevice , you must call QIODevice.seek() at the start of your function to ensure integrity with PySide.QtCore.QIODevice ‘s built-in buffer. The base implementation always returns true.

    PySide.QtCore.QIODevice. setErrorString ( errorString )
    参数: errorString – unicode

    将最后发生的设备错误的人类可读描述设为 str .

    PySide.QtCore.QIODevice. setOpenMode ( openMode )
    参数: openMode PySide.QtCore.QIODevice.OpenMode
    PySide.QtCore.QIODevice. setTextModeEnabled ( enabled )
    参数: enabled PySide.QtCore.bool

    enabled 为 true,此函数设置 文本 标志在设备;否则 文本 标志被移除。此特征很有用,对于提供自定义行尾处理的类而言在 PySide.QtCore.QIODevice .

    IO 设备应被打开,在调用此函数之前。

    PySide.QtCore.QIODevice. size ( )
    返回类型: PySide.QtCore.qint64

    对于打开的随机访问设备,此函数返回设备的大小。对于打开的顺序设备, PySide.QtCore.QIODevice.bytesAvailable() 被返回。

    若设备是关闭的,返回尺寸不会反映设备的实际大小。

    PySide.QtCore.QIODevice. ungetChar ( c )
    参数: c PySide.QtCore.char

    放置字符 c back into the device, and decrements the current position unless the position is 0. This function is usually called to “undo” a PySide.QtCore.QIODevice.getChar() operation, such as when writing a backtracking parser.

    c 先前未从设备读取,行为未定义。

    PySide.QtCore.QIODevice. waitForBytesWritten ( msecs )
    参数: msecs PySide.QtCore.int
    返回类型: PySide.QtCore.bool

    对于缓冲设备,此函数等待直到缓冲写入数据负载已写入设备且 PySide.QtCore.QIODevice.bytesWritten() signal has been emitted, or until msecs 毫秒已过去。若 msecs 为 -1,此函数不会超时。对于无缓冲设备,会立即返回。

    Returns true if a payload of data was written to the device; otherwise returns false (i.e. if the operation timed out, or if an error occurred).

    可以在没有事件循环的情况下操作此函数。它很有用,当编写非 GUI 应用程序和在非 GUI 线程中履行 I/O 操作时。

    若调用来自的槽有连接到 PySide.QtCore.QIODevice.bytesWritten() signal, PySide.QtCore.QIODevice.bytesWritten() will not be reemitted.

    Reimplement this function to provide a blocking API for a custom device. The default implementation does nothing, and returns false.

    警告

    从主 GUI 线程调用此函数可能导致用户界面被冻结。

    PySide.QtCore.QIODevice. waitForReadyRead ( msecs )
    参数: msecs PySide.QtCore.int
    返回类型: PySide.QtCore.bool

    阻塞直到有新的数据可供读取且 PySide.QtCore.QIODevice.readyRead() signal has been emitted, or until msecs 毫秒已过去。若 msecs 为 -1,此函数不会超时。

    Returns true if new data is available for reading; otherwise returns false (if the operation timed out or if an error occurred).

    可以在没有事件循环的情况下操作此函数。它很有用,当编写非 GUI 应用程序和在非 GUI 线程中履行 I/O 操作时。

    若调用来自的槽有连接到 PySide.QtCore.QIODevice.readyRead() signal, PySide.QtCore.QIODevice.readyRead() will not be reemitted.

    Reimplement this function to provide a blocking API for a custom device. The default implementation does nothing, and returns false.

    警告

    从主 GUI 线程调用此函数可能导致用户界面被冻结。

    PySide.QtCore.QIODevice. write ( data )
    参数: data PySide.QtCore.QByteArray
    返回类型: PySide.QtCore.qint64

    这是重载函数。

    写入内容为 byteArray 到设备。返回实际写入字节数;或 -1,若发生错误。

    PySide.QtCore.QIODevice. writeData ( data , len )
    参数:
    • data – str
    • len PySide.QtCore.qint64
    返回类型:

    PySide.QtCore.qint64

    写入直到 maxSize 字节来自 data 到设备。返回写入字节数,或 -1 若发生错误。

    此函数被调用由 PySide.QtCore.QIODevice 。重实现此函数,当创建子类为 PySide.QtCore.QIODevice .

    当重实现此函数时,此函数在返回前写入所有可用数据很重要。这是必需的为使 PySide.QtCore.QDataStream 能够在类中运转。 PySide.QtCore.QDataStream 假定所有信息已写入,因此不会试着再写入若存在问题。