QIODevice 類

The QIODevice 類是 Qt 中所有 I/O 設備的基接口類。 更多...

頭: #include <QIODevice>
繼承: QObject
繼承者: Q3Socket , Q3SocketDevice , QAbstractSocket , QBuffer , QFile , QLocalSocket , QNetworkReply ,和 QProcess

注意: 此類的所有函數 可重入 .

公共類型

flags OpenMode
enum OpenModeFlag { NotOpen, ReadOnly, WriteOnly, ReadWrite, ..., Unbuffered }

公共函數

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 ()
QString errorString () const
bool getChar (char * c )
bool isOpen () const
bool isReadable () const
virtual bool isSequential () const
bool isTextModeEnabled () const
bool isWritable () const
virtual bool open (OpenMode mode )
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 ()
qint64 readLine (char * data , qint64 maxSize )
QByteArray readLine (qint64 maxSize = 0)
virtual bool reset ()
virtual bool seek (qint64 pos )
void setTextModeEnabled (bool enabled )
virtual qint64 size () const
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 )

信號

void aboutToClose ()
void bytesWritten (qint64 bytes )
void readChannelFinished ()
void readyRead ()

保護函數

virtual qint64 readData (char * data , qint64 maxSize ) = 0
virtual qint64 readLineData (char * data , qint64 maxSize )
void setErrorString (const QString & str )
void setOpenMode (OpenMode openMode )
virtual qint64 writeData (const char * data , qint64 maxSize ) = 0

額外繼承成員

詳細描述

The QIODevice 類是 Qt 中所有 I/O 設備的基接口類。

QIODevice 為支持數據塊讀寫的設備提供公共實現和抽象接口,譬如 QFile , QBuffer and QTcpSocket . QIODevice 是抽象的且不可以被實例化,但通常使用它定義的接口以提供設備無關 I/O 特徵。例如,Qt 的 XML 類運轉於 QIODevice 指針,允許它們與各種設備 (譬如:文件和緩衝) 一起使用。

在訪問設備之前, open () 必須被調用以設置正確 OpenMode (譬如 ReadOnly or ReadWrite )。然後,可以寫入設備采用 write () 或 putChar (),和讀取通過調用 read (), readLine (),或 readAll ()。調用 close (),當設備用完時。

QIODevice 分 2 種類型的設備:隨機訪問設備和順序設備。

  • 隨機訪問設備支持尋址到任意位置使用 seek ()。可以獲得文件的當前位置通過調用 pos (). QFile and QBuffer 是隨機訪問設備範例。
  • 順序設備不支持尋址到任意位置。數據必須被一次性讀取。函數 pos () 和 size () 不工作對於順序設備。 QTcpSocket and QProcess 是順序設備範例。

可以使用 isSequential () 確定設備類型。

QIODevice 發射 readyRead () 當有新數據可供讀取時;例如,若新數據到達網絡,或額外數據被追加到正讀取的文件中。可以調用 bytesAvailable () 以確定目前可供讀取的字節數。常用 bytesAvailable () 連同 readyRead () 信號當采用異步設備編程時,譬如 QTcpSocket ,數據片段可以在任意時間點到達。 QIODevice 發射 bytesWritten () 信號,每當把數據負載寫入設備時。使用 bytesToWrite () 以確定當前等待寫入的數據量。

某些子類化的 QIODevice ,譬如 QTcpSocket and QProcess , 是異步的。這意味著 I/O 函數,譬如 write () 或 read () 總是立即返迴,而與設備本身的通信則可能發生,當控製返迴給事件循環時。 QIODevice 提供的函數允許強製立即履行這些操作,同時阻塞調用綫程且不進入事件循環。這允許 QIODevice 子類在沒有事件循環的情況下 (或在單獨綫程中) 使用:

  • waitForReadyRead () - 此函數掛起調用綫程中的操作,直到有新數據可供讀取。
  • waitForBytesWritten () - 此函數掛起調用綫程中的操作,直到將某一數據負載寫入設備為止。
  • waitFor....() - 子類化的 QIODevice 為特定設備操作實現阻塞功能。例如, QProcess has a function called waitForStarted() which suspends operation in the calling thread until the process has started.

從主 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();
					

通過子類化 QIODevice ,可以為自己的 I/O 設備提供相同接口。子類化的 QIODevice 隻要求實現保護 readData () 和 writeData () 函數。 QIODevice 使用這些函數以實現其所有方便函數,譬如 getChar (), readLine () 和 write (). QIODevice 還為您處理訪問控製,所以可以安全地假定設備以寫入模式打開,若 writeData () 被調用。

某些子類,譬如 QFile and QTcpSocket ,使用內存緩衝實現數據的中間體存儲。這減少瞭要求設備的訪問調用 (經常非常慢) 數。緩衝使函數像 getChar () 和 putChar () 很快,因為它們可以運轉於內存緩衝,而不是直接運轉於設備本身。不管怎樣,某些 I/O 操作不能很好的操控緩衝。例如,若多個用戶打開同一設備並逐字符讀取,它們最終可能讀取相同數據,當它們打算讀取各單獨組塊時。齣於此原因, QIODevice 允許繞過任何緩衝通過將 Unbuffered 標誌傳遞給 open ()。當子類化 QIODevice ,記得繞過可以使用的任何緩衝,當設備以 Unbuffered 模式打開時。

另請參閱 QBuffer , QFile ,和 QTcpSocket .

成員類型文檔編製

enum QIODevice:: OpenModeFlag
flags QIODevice:: OpenMode

此枚舉用於 open () 以描述設備被打開的模式。它也被返迴由 openMode ().

常量 描述
QIODevice::NotOpen 0x0000 設備未打開。
QIODevice::ReadOnly 0x0001 打開設備以供讀取。
QIODevice::WriteOnly 0x0002 The device is open for writing.
QIODevice::ReadWrite ReadOnly | WriteOnly 打開設備以供讀寫。
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 若可能,會截取設備在它被打開之前。設備的所有早期內容將丟失。
QIODevice::Text 0x0010 當讀取時,把行尾終止符轉換成 \n。當寫入時,把行尾終止符轉換成本地編碼 (例如:用於 Win32 的 \r\n)。
QIODevice::Unbuffered 0x0020 繞過任何設備緩衝。

某些標誌,譬如 Unbuffered and Truncate 沒有意義,當用於某些子類時。其中某些限定由子類錶示的設備類型所隱含。在其它情況下,限定可能是由於實現,或可能是通過底層平颱施加;例如, QTcpSocket 不支持 Unbuffered 模式,且局限在本機 API 以阻止 QFile 從支持 Unbuffered 在 Windows。

OpenMode 類型是 typedef 對於 QFlags <OpenModeFlag>。它存儲 OpenModeFlag 值的 OR 組閤。

成員函數文檔編製

QIODevice:: QIODevice ()

構造 QIODevice 對象。

QIODevice:: QIODevice ( QObject * parent )

構造 QIODevice 對象采用給定 parent .

[虛擬] QIODevice:: ~QIODevice ()

析構函數是虛擬的,且 QIODevice 是抽象基類。此析構函數不調用 close (),但子類析構函數可能會。若有疑問,調用 close () 之後銷毀 QIODevice .

[signal] void QIODevice:: aboutToClose ()

此信號被發射當設備即將關閉時。連接此信號,若需要履行操作在設備關閉之前 (如:若在需要寫入設備的單獨緩衝中有數據)。

[虛擬] bool QIODevice:: atEnd () const

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.

對於某些設備,atEnd() 可以返迴 true 即使有更多數據要讀取。此特殊情況僅適用於設備生成數據以直接響應調用 read () (如, /dev or /proc files on Unix and Mac OS X, or console input / stdin 在所有平颱)。

另請參閱 bytesAvailable (), read (),和 isSequential ().

[虛擬] qint64 QIODevice:: bytesAvailable () const

返迴可供讀取的可用字節數。此函數通常用於順序設備,以確定在讀取之前緩衝中分配的字節數。

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

qint64 CustomDevice::bytesAvailable() const
{
    return buffer.size() + QIODevice::bytesAvailable();
}
					

另請參閱 bytesToWrite (), readyRead (),和 isSequential ().

[虛擬] qint64 QIODevice:: bytesToWrite () const

對於緩衝設備,此函數返迴等待寫入的字節數。對於沒有緩衝的設備,此函數返迴 0。

另請參閱 bytesAvailable (), bytesWritten (),和 isSequential ().

[signal] void QIODevice:: bytesWritten ( qint64 bytes )

此信號發射,每當把數據負載寫入設備時。 bytes 自變量是在此負載中寫入的設置字節數。

bytesWritten() 不會遞歸發射;若重新進入事件循環或調用 waitForBytesWritten () 在連接到 bytesWritten() 信號的槽內,信號不會被重新發射 (盡管 waitForBytesWritten () 可能仍返迴 true)。

另請參閱 readyRead ().

[虛擬] bool QIODevice:: canReadLine () const

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

注意:沒有辦法確定是否可以讀取的無緩衝設備始終返迴 false。

此函數經常被調用結閤 readyRead () 信號。

重實現此函數的子類必須調用基實現以便包括內容為 QIODevice 的緩衝。範例:

bool CustomDevice::canReadLine() const
{
    return buffer.contains('\n') || QIODevice::canReadLine();
}
					

另請參閱 readyRead () 和 readLine ().

[虛擬] void QIODevice:: close ()

首先發射 aboutToClose (),然後關閉設備並設置其 OpenMode to NotOpen 。錯誤字符串也會被重置。

另請參閱 setOpenMode () 和 OpenMode .

QString QIODevice:: errorString () const

返迴人類可讀的最後發生的設備錯誤描述。

另請參閱 setErrorString ().

bool QIODevice:: getChar ( char * c )

從設備讀取 1 字符並把它存儲在 c 。若 c is 0, the character is discarded. Returns true on success; otherwise returns false.

另請參閱 read (), putChar (),和 ungetChar ().

bool QIODevice:: isOpen () const

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 openMode () 返迴 NotOpen .

另請參閱 openMode () 和 OpenMode .

bool QIODevice:: isReadable () const

Returns true if data can be read from the device; otherwise returns false. Use bytesAvailable () 以確定可以讀取多少字節。

這是方便校驗函數若 OpenMode 的設備包含 ReadOnly 標誌。

另請參閱 openMode () 和 OpenMode .

[虛擬] bool QIODevice:: isSequential () const

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

與隨機訪問設備相反,順序設備沒有開始、結束、大小或當前位置的概念,且它們不支持尋址。纔可以從設備讀取數據,僅當設備報告數據可用時。順序設備的最常見範例是網絡套接字。在 Unix,特殊文件 (譬如 /dev/zero 和 fifo 管道) 是順序的。

另一方麵,常規文件確實支持隨機訪問。它們擁有大小和當前位置,且它們還支持在數據流中嚮後和嚮前尋址。常規文件是非順序的。

The QIODevice 實現返迴 false。

另請參閱 bytesAvailable ().

bool QIODevice:: isTextModeEnabled () const

返迴 true 若 Text flag is enabled; otherwise returns false.

另請參閱 setTextModeEnabled ().

bool QIODevice:: isWritable () const

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

這是方便校驗函數若 OpenMode 的設備包含 WriteOnly 標誌。

另請參閱 openMode () 和 OpenMode .

[虛擬] bool QIODevice:: open ( OpenMode mode )

打開設備並設置其 OpenMode to mode . Returns true if successful; otherwise returns false. This function should be called from any reimplementations of open() or other functions that open the device.

另請參閱 openMode () 和 OpenMode .

OpenMode QIODevice:: openMode () const

返迴設備被打開的模式;即: ReadOnly or WriteOnly .

另請參閱 setOpenMode () 和 OpenMode .

qint64 QIODevice:: peek ( char * data , qint64 maxSize )

讀取最多 maxSize 字節從設備到 data ,無副作用 (即:若調用 read () 在 peek() 之後,將獲得相同數據)。返迴讀取字節數。若齣現錯誤,譬如:當試圖窺視打開設備以 WriteOnly 模式,此函數返迴 -1。

返迴 0,當沒有更多數據可供讀取時。

範例:

bool isExeFile(QFile *file)
{
    char buf[2];
    if (file->peek(buf, sizeof(buf)) == sizeof(buf))
        return (buf[0] == 'M' && buf[1] == 'Z');
    return false;
}
					

該函數在 Qt 4.1 引入。

另請參閱 read ().

QByteArray QIODevice:: peek ( qint64 maxSize )

這是重載函數。

窺視最多 maxSize 字節從設備,返迴窺視數據作為 QByteArray .

範例:

bool isExeFile(QFile *file)
{
    return file->peek(2) == "MZ";
}
					

This function has no way of reporting errors; returning an empty QByteArray() can mean either that no data was currently available for peeking, or that an error occurred.

該函數在 Qt 4.1 引入。

另請參閱 read ().

[虛擬] qint64 QIODevice:: pos () const

對於隨機訪問設備,此函數返迴要寫入或讀取數據的位置。對於沒有當前位置概念的順序設備或閉閤設備,返迴 0。

設備的當前讀/寫位置的內部維護由 QIODevice ,因此重實現此函數不必要。當子類化 QIODevice ,使用 QIODevice::seek () 來通知 QIODevice 關於設備位置的變化。

另請參閱 isSequential () 和 seek ().

bool QIODevice:: putChar ( char c )

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

另請參閱 write (), getChar (),和 ungetChar ().

qint64 QIODevice:: read ( char * data , qint64 maxSize )

讀取最多 maxSize 字節從設備到 data ,並返迴讀取字節數。若齣現錯誤,譬如試圖讀取的設備被打開以 WriteOnly 模式,此函數返迴 -1。

返迴 0 當沒有更多可供讀取的數據時。無論如何,認為讀取流的末尾是錯誤的,所以,在此情況 (即:在關閉的套接字上讀取,或在進程已死亡之後讀取) 下,此函數返迴 -1。

另請參閱 readData (), readLine (),和 write ().

QByteArray QIODevice:: read ( qint64 maxSize )

這是重載函數。

讀取最多 maxSize 字節從設備,並把讀取數據返迴作為 QByteArray .

This function has no way of reporting errors; returning an empty QByteArray() can mean either that no data was currently available for reading, or that an error occurred.

QByteArray QIODevice:: readAll ()

這是重載函數。

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

This function has no way of reporting errors; returning an empty QByteArray() can mean either that no data was currently available for reading, or that an error occurred.

[signal] void QIODevice:: readChannelFinished ()

此信號被發射當關閉此設備的輸入 (讀取) 流時。一檢測到關閉就盡快發齣它,這意味著可能仍有數據可供讀取采用 read ().

該函數在 Qt 4.4 引入。

另請參閱 atEnd () 和 read ().

[pure virtual protected] qint64 QIODevice:: readData ( char * data , qint64 maxSize )

讀取到 maxSize 字節從設備到 data ,並返迴讀取字節數;返迴 -1 若發生錯誤。

若沒有字節要讀取且從不會有更多字節可用 (範例包括:套接字被關閉、管道被關閉、子進程已完成),此函數返迴 -1。

此函數被調用通過 QIODevice 。重實現此函數,當創建子類為 QIODevice .

當重實現此函數時,此函數在返迴之前讀取所有要求數據是很重要的。這是必需的為 QDataStream 能夠運轉於類。 QDataStream 假定讀取瞭所有請求信息,因此,不會試著讀取 (若存在問題)。

另請參閱 read (), readLine (),和 writeData ().

qint64 QIODevice:: readLine ( char * data , qint64 maxSize )

此函數從設備讀取一行 ASCII 字符,直到最多 maxSize - 1 字節,將字符存儲在 data ,並返迴讀取字節數。若無法讀取一行但未發生錯誤,此函數返迴 0。若發生錯誤,此函數返迴可以讀取的長度,或 -1 若什麼都未讀取。

終止 \0 字節始終被追加到 data ,所以 maxSize 必須大於 1。

讀取數據直到滿足以下任一條件:

  • 第一 \n 字符被讀取。
  • maxSize - 1 字節被讀取。
  • 檢測到設備數據結束。

例如,以下代碼從文件讀取一行字符:

QFile file("box.txt");
if (file.open(QFile::ReadOnly)) {
    char buf[1024];
    qint64 lineLength = file.readLine(buf, sizeof(buf));
    if (lineLength != -1) {
        // the line is available in buf
    }
}
					

換行符 \n 包括在緩衝中。若在讀取 maxSize -1 字節之前未遭遇換行符,則不會將換行符插入緩衝。在 Windows,采用 \n 替換換行符。

此函數調用 readLineData (),這的實現是使用重復調用對 getChar ()。可以提供更高效實現通過重實現 readLineData () 在自己的子類中。

另請參閱 getChar (), read (),和 write ().

QByteArray QIODevice:: readLine ( qint64 maxSize = 0)

這是重載函數。

從設備讀取行,但不超過 maxSize characters, and returns the result as a QByteArray .

This function has no way of reporting errors; returning an empty QByteArray() can mean either that no data was currently available for reading, or that an error occurred.

[virtual protected] qint64 QIODevice:: readLineData ( char * data , qint64 maxSize )

讀取到 maxSize 字符到 data 並返迴讀取的字符數。

此函數被調用通過 readLine (),並提供其基實現,使用 getChar ()。緩衝設備可以改善性能對於 readLine () 通過重實現此函數。

readLine () 追加 \0 字節到 data ;readLineData() 不需要這樣做。

若重實現此函數,小心返迴正確值:它應返迴在這一行中讀取的字節數 (包括終止換行符),或 0 若此時沒有行要讀取。若齣現錯誤,應返迴 -1,當且僅當沒有字節要讀取時。讀取超過 EOF 認為齣錯。

[signal] void QIODevice:: readyRead ()

This signal is emitted once every time new data is available for reading from the device. It will only be emitted again once new data is available, such as when a new payload of network data has arrived on your network socket, or when a new block of data has been appended to your device.

readyRead() 不會遞歸發射;若重新進入事件循環或調用 waitForReadyRead () 在連接到 readyRead() 信號的槽內,信號不會被重新發射 (盡管 waitForReadyRead () 可能仍返迴 true)。

注意,開發者實現的類派生自 QIODevice :應始終發射 readyRead() 當有新數據到達時 (不要僅因為緩衝中仍有數據要讀取而發射它)。在其它條件下不要發射 readyRead()。

另請參閱 bytesWritten ().

[虛擬] bool QIODevice:: reset ()

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).

注意,當使用 QTextStream QFile ,調用 reset() 在 QFile 將不會有期望結果,因為 QTextStream 緩衝文件。使用 QTextStream::seek () 函數代替。

另請參閱 seek ().

[虛擬] bool QIODevice:: seek ( qint64 pos )

對於隨機訪問設備,此函數將當前位置設為 pos ,返迴 true 當成功時,或 false 若齣現錯誤。對於順序設備,默認行為是什麼都不做並返迴 false。

當子類化 QIODevice ,必須在函數開始時調用 QIODevice::seek() 以確保完整性采用 QIODevice 's built-in buffer. The base implementation always returns true.

另請參閱 pos () 和 isSequential ().

[protected] void QIODevice:: setErrorString (const QString & str )

將最後發生的設備錯誤的人類可讀描述設為 str .

另請參閱 errorString ().

[protected] void QIODevice:: setOpenMode ( OpenMode openMode )

設置 OpenMode 為設備到 openMode 。調用此函數以設置打開模式,若設備打開後標誌有變化。

另請參閱 openMode () 和 OpenMode .

void QIODevice:: setTextModeEnabled ( bool enabled )

enabled 為 true,此函數設置 Text 標誌在設備;否則 Text 標誌被移除。此特徵很有用,對於提供自定義行尾處理的類而言在 QIODevice .

IO 設備應被打開,在調用此函數之前。

另請參閱 isTextModeEnabled (), open (),和 setOpenMode ().

[虛擬] qint64 QIODevice:: size () const

對於打開的隨機訪問設備,此函數返迴設備的大小。對於打開的順序設備, bytesAvailable () 被返迴。

若設備是關閉的,返迴尺寸不會反映設備的實際大小。

另請參閱 isSequential () 和 pos ().

void QIODevice:: ungetChar ( char c )

放置字符 c 迴到設備,並遞減當前位置除非位置為 0。此函數通常被稱為撤銷 getChar () 操作,譬如:當編寫迴溯剖析器時。

c 先前未從設備讀取,行為未定義。

[虛擬] bool QIODevice:: waitForBytesWritten ( int msecs )

對於緩衝設備,此函數等待直到緩衝寫入數據負載已寫入設備且 bytesWritten () 信號已發射,或直到 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 操作時。

若調用來自的槽有連接到 bytesWritten () 信號, bytesWritten () 不會重新發射。

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

警告: 從主 GUI 綫程調用此函數可能導緻用戶界麵被凍結。

另請參閱 waitForReadyRead ().

[虛擬] bool QIODevice:: waitForReadyRead ( int msecs )

阻塞直到有新的數據可供讀取且 readyRead () 信號已發射,或直到 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 操作時。

若調用來自的槽有連接到 readyRead () 信號, readyRead () 不會重新發射。

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

警告: 從主 GUI 綫程調用此函數可能導緻用戶界麵被凍結。

另請參閱 waitForBytesWritten ().

qint64 QIODevice:: write (const char * data , qint64 maxSize )

寫入最多 maxSize 字符的數據從 data 到設備。返迴實際寫入字節數;或 -1,若發生錯誤。

另請參閱 read () 和 writeData ().

qint64 QIODevice:: write (const char * data )

這是重載函數。

把數據從 8 位字符的零終止字符串寫入設備。返迴實際寫入字節數;或 -1,若發生錯誤。這相當於

...
QIODevice::write(data, qstrlen(data));
...
					

該函數在 Qt 4.5 引入。

另請參閱 read () 和 writeData ().

qint64 QIODevice:: write (const QByteArray & byteArray )

這是重載函數。

寫入內容為 byteArray 到設備。返迴實際寫入字節數;或 -1,若發生錯誤。

另請參閱 read () 和 writeData ().

[pure virtual protected] qint64 QIODevice:: writeData (const char * data , qint64 maxSize )

寫入直到 maxSize 字節來自 data 到設備。返迴寫入字節數,或 -1 若發生錯誤。

此函數被調用通過 QIODevice 。重實現此函數,當創建子類為 QIODevice .

當重實現此函數時,此函數在返迴前寫入所有可用數據很重要。這是必需的為使 QDataStream 能夠運轉於類。 QDataStream 假定寫入瞭所有信息,因此,不會試著寫入 (若存在問題)。

另請參閱 read () 和 write ().