QTextStream 類

QTextStream 類提供用於讀寫文本的方便接口。 更多...

頭: #include <QTextStream>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
繼承: QIODeviceBase

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

公共類型

enum FieldAlignment { AlignLeft, AlignRight, AlignCenter, AlignAccountingStyle }
enum NumberFlag { ShowBase, ForcePoint, ForceSign, UppercaseBase, UppercaseDigits }
flags NumberFlags
enum RealNumberNotation { ScientificNotation, FixedNotation, SmartNotation }
enum Status { Ok, ReadPastEnd, ReadCorruptData, WriteFailed }

公共函數

QTextStream ()
QTextStream (QIODevice * device )
QTextStream (FILE * fileHandle , QIODeviceBase::OpenMode openMode = ReadWrite)
QTextStream (QByteArray * array , QIODeviceBase::OpenMode openMode = ReadWrite)
QTextStream (QString * string , QIODeviceBase::OpenMode openMode = ReadWrite)
QTextStream (const QByteArray & array , QIODeviceBase::OpenMode openMode = ReadOnly)
virtual ~QTextStream ()
bool atEnd () const
bool autoDetectUnicode () const
QIODevice * device () const
QStringConverter::Encoding encoding () const
QTextStream::FieldAlignment fieldAlignment () const
int fieldWidth () const
void flush ()
bool generateByteOrderMark () const
int integerBase () const
QLocale locale () const
QTextStream::NumberFlags numberFlags () const
QChar padChar () const
qint64 pos () const
QString read (qint64 maxlen )
QString readAll ()
QString readLine (qint64 maxlen = 0)
bool readLineInto (QString * line , qint64 maxlen = 0)
QTextStream::RealNumberNotation realNumberNotation () const
int realNumberPrecision () const
void reset ()
void resetStatus ()
bool seek (qint64 pos )
void setAutoDetectUnicode (bool enabled )
void setDevice (QIODevice * device )
void setEncoding (QStringConverter::Encoding encoding )
void setFieldAlignment (QTextStream::FieldAlignment mode )
void setFieldWidth (int width )
void setGenerateByteOrderMark (bool generate )
void setIntegerBase (int base )
void setLocale (const QLocale & locale )
void setNumberFlags (QTextStream::NumberFlags flags )
void setPadChar (QChar ch )
void setRealNumberNotation (QTextStream::RealNumberNotation notation )
void setRealNumberPrecision (int precision )
void setStatus (QTextStream::Status status )
void setString (QString * string , QIODeviceBase::OpenMode openMode = ReadWrite)
void skipWhiteSpace ()
QTextStream::Status status () const
QString * string () const
QTextStream & operator<< (QChar c )
QTextStream & operator<< (const QString & string )
QTextStream & operator<< (float f )
QTextStream & operator<< (short i )
QTextStream & operator<< (QLatin1StringView string )
QTextStream & operator<< (QStringView string )
QTextStream & operator<< (char c )
(since 6.3.1) QTextStream & operator<< (char16_t c )
QTextStream & operator<< (const QByteArray & array )
QTextStream & operator<< (const char * string )
QTextStream & operator<< (const void * ptr )
QTextStream & operator<< (double f )
QTextStream & operator<< (int i )
QTextStream & operator<< (long i )
QTextStream & operator<< (qlonglong i )
QTextStream & operator<< (qulonglong i )
QTextStream & operator<< (unsigned int i )
QTextStream & operator<< (unsigned long i )
QTextStream & operator<< (unsigned short i )
QTextStream & operator>> (QChar & c )
QTextStream & operator>> (QString & str )
QTextStream & operator>> (float & f )
QTextStream & operator>> (short & i )
QTextStream & operator>> (QByteArray & array )
QTextStream & operator>> (char & c )
QTextStream & operator>> (char * c )
(從 6.4 起) QTextStream & operator>> (char16_t & c )
QTextStream & operator>> (double & f )
QTextStream & operator>> (int & i )
QTextStream & operator>> (long & i )
QTextStream & operator>> (qlonglong & i )
QTextStream & operator>> (qulonglong & i )
QTextStream & operator>> (unsigned int & i )
QTextStream & operator>> (unsigned long & i )
QTextStream & operator>> (unsigned short & i )
QTextStreamManipulator qSetFieldWidth (int width )
QTextStreamManipulator qSetPadChar (QChar ch )
QTextStreamManipulator qSetRealNumberPrecision (int precision )

詳細描述

QTextStream 可以運轉於 QIODevice QByteArray QString 。使用 QTextStream 的流運算符,可以方便地讀寫單詞、行及數字。為生成文本,QTextStream 支持用於字段鋪墊和對齊的格式化選項、及數字格式化。範例:

QFile data("output.txt");
if (data.open(QFile::WriteOnly | QFile::Truncate)) {
    QTextStream out(&data);
    out << "Result: " << qSetFieldWidth(10) << left << 3.14 << 2.7;
    // writes "Result: 3.14      2.7       "
}
					

使用 QTextStream 讀取控製颱輸入,並寫入控製颱輸齣也很常見。QTextStream 感知區域設置,且會使用正確編碼自動解碼 stdin (標準輸入)。範例:

QTextStream stream(stdin);
QString line;
while (stream.readLineInto(&line)) {
    ...
}
					

除使用 QTextStream 構造函數外,還可以設置操作設備 (或字符串) 的 QTextStream 通過調用 setDevice () 或 setString ()。可以尋址到位置通過調用 seek (),和 atEnd () 將返迴 true 當沒有剩餘數據要讀取時。若調用 flush (),QTextStream 將清空其寫入緩衝中的所有數據並調用 flush () 在設備。

在內部,QTextStream 使用 Unicode 基緩衝,和 QStringConverter 用於 QTextStream 以自動支持不同編碼。默認情況下,UTF-8 用於讀寫,但也可以設置編碼通過調用 setEncoding ()。自動 Unicode 檢測也支持。當啓用此特徵 (默認行為) 時,QTextStream 將檢測 UTF-8、UTF-16 或 UTF-32 BOM (字節序標記),並在讀取時切換到適當 UTF 編碼。默認情況下,QTextStream 不會寫入 BOM,但可以啓用這通過調用 setGenerateByteOrderMark (true)。當 QTextStream 運轉於 QString 直接,禁用編碼。

使用 QTextStream 有 3 種一般辦法,當讀取文本文件時:

  • 逐個分塊,通過調用 readLine () 或 readAll ().
  • 逐個單詞。QTextStream 支持流化成 QString s, QByteArray 及 char* 緩衝。單詞由空格定界,並自動跳過前導空白。
  • 逐個字符,通過流化成 QChar 或 char 類型。此方法經常用於方便處理輸入當剖析文件時,獨立於字符編碼和行尾語義。要跳過空白,調用 skipWhiteSpace ().

由於文本流使用緩衝,不應該使用超類實現從流讀取。例如,若擁有 QFile 並直接讀取自它使用 QFile::readLine () 而不是使用流,文本流的內部位置將不同步於文件位置。

默認情況下,當從文本流讀取數字時,QTextStream 將自動檢測數字的基錶示。例如,若數字以 0x 開始,假定是十六進製形式。若以數字 1-9 開始,假定是十進製形式,依此類推。可以設置整數基,從而禁用自動檢測,通過調用 setIntegerBase ()。範例:

QTextStream in("0x50 0x20");
int firstNumber, secondNumber;
in >> firstNumber;             // firstNumber == 80
in >> dec >> secondNumber;     // secondNumber == 0
char ch;
in >> ch;                      // ch == 'x'
					

QTextStream 支持用於生成文本的很多格式化選項。可以設置字段寬度,和填充字符通過調用 setFieldWidth () 和 setPadChar ()。使用 setFieldAlignment () 以設置每個字段內的對齊方式。對於實數,調用 setRealNumberNotation () 和 setRealNumberPrecision () 以設置錶示法 ( SmartNotation , ScientificNotation , FixedNotation ) 及生成數字的位數精度。一些額外數字格式化選項也是可用的透過 setNumberFlags ().

<iostream> 在標準 C++ 庫,QTextStream 還定義瞭幾個全局操縱器函數:

操作符 描述
Qt::bin 如同 setIntegerBase (2).
Qt::oct 如同 setIntegerBase (8).
Qt::dec 如同 setIntegerBase (10).
Qt::hex 如同 setIntegerBase (16).
Qt::showbase 如同 setNumberFlags ( numberFlags () | ShowBase ).
Qt::forcesign 如同 setNumberFlags ( numberFlags () | ForceSign ).
Qt::forcepoint 如同 setNumberFlags ( numberFlags () | ForcePoint ).
Qt::noshowbase 如同 setNumberFlags ( numberFlags () & ~ ShowBase ).
Qt::noforcesign 如同 setNumberFlags ( numberFlags () & ~ ForceSign ).
Qt::noforcepoint 如同 setNumberFlags ( numberFlags () & ~ ForcePoint ).
Qt::uppercasebase 如同 setNumberFlags ( numberFlags () | UppercaseBase ).
Qt::uppercasedigits 如同 setNumberFlags ( numberFlags () | UppercaseDigits ).
Qt::lowercasebase 如同 setNumberFlags ( numberFlags () & ~ UppercaseBase ).
Qt::lowercasedigits 如同 setNumberFlags ( numberFlags () & ~ UppercaseDigits ).
Qt::fixed 如同 setRealNumberNotation ( FixedNotation ).
Qt::scientific 如同 setRealNumberNotation ( ScientificNotation ).
Qt::left 如同 setFieldAlignment ( AlignLeft ).
Qt::right 如同 setFieldAlignment ( AlignRight ).
Qt::center 如同 setFieldAlignment ( AlignCenter ).
Qt::endl 如同運算符 <<('\n') 和 flush ().
Qt::flush 如同 flush ().
Qt::reset 如同 reset ().
Qt::ws 如同 skipWhiteSpace ().
Qt::bom 如同 setGenerateByteOrderMark (true)。

此外,Qt 提供瞭 3 個接受參數的全局操縱符: qSetFieldWidth (), qSetPadChar (),和 qSetRealNumberPrecision ().

另請參閱 QDataStream , QIODevice , QFile , QBuffer ,和 QTcpSocket .

成員類型文檔編製

enum QTextStream:: FieldAlignment

此枚舉指定如何對齊字段中的文本,當字段比占據文本更寬時。

常量 描述
QTextStream::AlignLeft 0 鋪墊在字段右側。
QTextStream::AlignRight 1 鋪墊在字段左側。
QTextStream::AlignCenter 2 鋪墊在字段的兩側。
QTextStream::AlignAccountingStyle 3 如同 AlignRight,除刷新數字符號左側外。

另請參閱 setFieldAlignment ().

enum QTextStream:: NumberFlag
flags QTextStream:: NumberFlags

此枚舉指定可以設置的各種標誌能影響輸齣對於整數, float ,和 double

常量 描述
QTextStream::ShowBase 0x1 展示作為前綴的基,若基為 16 (0x)、8 (0)、或 2 (0b)。
QTextStream::ForcePoint 0x2 在數字中始終放置小數分隔符,即使沒有小數。
QTextStream::ForceSign 0x4 在數字中始終放置符號,即使為正數。
QTextStream::UppercaseBase 0x8 使用大寫版本的基前綴 (0X、0B)。
QTextStream::UppercaseDigits 0x10 數字 10-35 使用大寫字母錶達,而不是小寫。

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

另請參閱 setNumberFlags ().

enum QTextStream:: RealNumberNotation

此枚舉指定使用哪種錶示法錶達 float and double 以字符串形式。

常量 描述
QTextStream::ScientificNotation 2 科學錶示法 ( printf() 's %e 標誌)。
QTextStream::FixedNotation 1 定點錶示法 ( printf() 's %f 標誌)。
QTextStream::SmartNotation 0 科學 (或定點) 錶示法,從屬哪種更有意義 ( printf() 's %g 標誌)。

另請參閱 setRealNumberNotation ().

enum QTextStream:: Status

此枚舉描述文本流的當前狀態。

常量 描述
QTextStream::Ok 0 文本流運轉正常。
QTextStream::ReadPastEnd 1 文本流已讀取過瞭底層設備的數據末尾。
QTextStream::ReadCorruptData 2 文本流有讀取被破壞數據。
QTextStream::WriteFailed 3 文本流無法寫入到底層設備。

另請參閱 status ().

成員函數文檔編製

QTextStream:: QTextStream ()

構造 QTextStream。在可以使用它進行讀取 (或寫入) 之前,必須賦值設備 (或字符串)。

另請參閱 setDevice () 和 setString ().

[explicit] QTextStream:: QTextStream ( QIODevice * device )

構造 QTextStream 運轉於 device .

[explicit] QTextStream:: QTextStream ( FILE * fileHandle , QIODeviceBase::OpenMode openMode = ReadWrite)

構造 QTextStream 運轉於 fileHandle ,使用 openMode 定義打開方式。在內部, QFile 被創建以處理 FILE 指針。

此構造函數很有用,對於直接工作於基於公共 FILE 的輸入和輸齣流:stdin、stdout 和 stderr。範例:

QString str;
QTextStream in(stdin);
in >> str;
					

[explicit] QTextStream:: QTextStream ( QByteArray * array , QIODeviceBase::OpenMode openMode = ReadWrite)

構造 QTextStream 運轉於 array ,使用 openMode 定義打開方式。在內部,數組的包裹是通過 QBuffer .

[explicit] QTextStream:: QTextStream ( QString * string , QIODeviceBase::OpenMode openMode = ReadWrite)

構造 QTextStream 運轉於 string ,使用 openMode 定義打開方式。

[explicit] QTextStream:: QTextStream (const QByteArray & array , QIODeviceBase::OpenMode openMode = ReadOnly)

構造 QTextStream 運轉於 array ,使用 openMode 定義打開方式。以隻讀方式訪問數組,不管值 openMode .

此構造函數操控常量字符串很方便。範例:

int main(int argc, char *argv[])
{
    // read numeric arguments (123, 0x20, 4.5...)
    for (int i = 1; i < argc; ++i) {
          int number;
          QTextStream in(argv[i]);
          in >> number;
          ...
    }
}
					

[virtual noexcept] QTextStream:: ~QTextStream ()

銷毀 QTextStream .

若流運轉於設備, flush () 會被隱式調用。否則,設備不受影響。

bool QTextStream:: atEnd () const

返迴 true 若沒有更多數據能讀取自 QTextStream ;否則返迴 false 。這類似於,但不同於調用 QIODevice::atEnd (),因為 QTextStream 還考慮其內部 Unicode 緩衝。

bool QTextStream:: autoDetectUnicode () const

返迴 true 若啓用瞭自動 Unicode 檢測,否則返迴 false 。默認啓用自動 Unicode 檢測。

另請參閱 setAutoDetectUnicode () 和 setEncoding ().

QIODevice *QTextStream:: device () const

返迴被當前設備關聯的 QTextStream ,或 nullptr 若沒有設備被賦值。

另請參閱 setDevice () 和 string ().

QStringConverter::Encoding QTextStream:: encoding () const

返迴賦值給流的當前編碼。

另請參閱 setEncoding (), setAutoDetectUnicode (),和 locale ().

QTextStream::FieldAlignment QTextStream:: fieldAlignment () const

返迴當前字段的對齊。

另請參閱 setFieldAlignment () 和 fieldWidth ().

int QTextStream:: fieldWidth () const

返迴當前字段的寬度。

另請參閱 setFieldWidth ().

void QTextStream:: flush ()

刷新等待寫入設備的任何緩衝數據。

QTextStream 運轉於字符串,此函數什麼都不做。

bool QTextStream:: generateByteOrderMark () const

返迴 true if QTextStream 被設為生成 UTF BOM (字節序標記),當使用 UTF 編碼時;否則返迴 false 。默認情況下,UTF BOM (字節序標記) 生成被設為 false。

另請參閱 setGenerateByteOrderMark ().

int QTextStream:: integerBase () const

返迴整數的當前基。0 意味著檢測基當讀取時,或 10 (十進製) 當生成數字時。

另請參閱 setIntegerBase (), QString::number (),和 numberFlags ().

QLocale QTextStream:: locale () const

返迴用於此流的區域設置。默認區域設置為 C。

另請參閱 setLocale ().

QTextStream::NumberFlags QTextStream:: numberFlags () const

返迴當前數字的標誌。

另請參閱 setNumberFlags (), integerBase (),和 realNumberNotation ().

QChar QTextStream:: padChar () const

返迴當前鋪墊的字符。

另請參閱 setPadChar () 和 setFieldWidth ().

qint64 QTextStream:: pos () const

返迴對應流當前位置的設備位置,或 -1 若齣現錯誤 (如:若沒有設備/字符串,或若存在設備錯誤)。

因為 QTextStream 有緩衝,此函數可能必須尋址設備以重構有效設備位置。此操作會很昂貴,所以,可能想要避免在緊密循環中調用此函數。

另請參閱 seek ().

QString QTextStream:: read ( qint64 maxlen )

讀取最多 maxlen 字符從流,並返迴讀取數據按 QString .

另請參閱 readAll (), readLine (),和 QIODevice::read ().

QString QTextStream:: readAll ()

讀取流的整個內容,並返迴它按 QString 。避免使用此函數,當操控大文件時。因為 , 它會消耗大量內存。

調用 readLine () 更好,若不知道有多少數據可用。

另請參閱 readLine ().

QString QTextStream:: readLine ( qint64 maxlen = 0)

從流讀取 1 行文本,並返迴它按 QString 。最大允許行長度被設為 maxlen 。若流包含的行長於這,則分割行後於 maxlen 字符並以部分形式返迴。

maxlen 為 0,行可以是任意長度。

返迴行沒有結尾 (\n 或 \r\n) 行尾字符,所以調用 QString::trimmed () 可能不必要。

若流已讀取到 EOF (文件末尾),readLine() 返迴 null QString 。對於字符串 (或支持字符串的設備),可以明確測試流是否結束使用 atEnd ().

另請參閱 readAll () 和 QIODevice::readLine ().

bool QTextStream:: readLineInto ( QString * line , qint64 maxlen = 0)

從流讀取 1 行文本到 line 。若 line is nullptr ,不存儲讀取行。

最大允許行長度被設為 maxlen 。若流包含的行長於這,則分割行後於 maxlen 字符並以部分形式返迴。

maxlen 為 0,行可以是任意長度。

結果行沒有結尾 (\n 或 \r\n) 行尾字符,所以調用 QString::trimmed () 可能不必要。

line 有足夠容量容納即將讀取的數據;此函數可能不需要分配新內存。因此,它可以更快相比 readLine ().

返迴 false 若流已讀取到 EOF (文件末尾) 或齣現錯誤;否則返迴 true 。內容在 line 在調用前被丟棄,在任何情況下。

另請參閱 readAll () 和 QIODevice::readLine ().

QTextStream::RealNumberNotation QTextStream:: realNumberNotation () const

返迴當前實數錶示法。

另請參閱 setRealNumberNotation (), realNumberPrecision (), numberFlags (),和 integerBase ().

int QTextStream:: realNumberPrecision () const

返迴當前實數的精度,或數字的小數位數, QTextStream 將寫入當生成實數 ( FixedNotation , ScientificNotation ),或最大數字的有效位數 ( SmartNotation ).

另請參閱 setRealNumberPrecision (), setRealNumberNotation (), realNumberNotation (), numberFlags (),和 integerBase ().

void QTextStream:: reset ()

重置 QTextStream 的格式化選項,將之還原到其原始構造狀態。設備、字符串及任何緩衝數據,保持不變。

void QTextStream:: resetStatus ()

重置文本流的狀態。

另請參閱 QTextStream::Status , status (),和 setStatus ().

bool QTextStream:: seek ( qint64 pos )

尋址到位置 pos 在設備中。返迴 true 當成功時;否則返迴 false .

void QTextStream:: setAutoDetectUnicode ( bool enabled )

enabled 為 True, QTextStream 將試圖通過窺視流數據以檢測 Unicode 編碼,看是否可以找到 UTF-8、UTF-16 或 UTF-32 BOM (字節序標記)。若有找到此標記, QTextStream 將以 UTF 編碼替換當前編碼。

此函數可以一起使用與 setEncoding ()。常見是把編碼設為 UTF-8,然後啓用 UTF-16 檢測。

另請參閱 autoDetectUnicode () 和 setEncoding ().

void QTextStream:: setDevice ( QIODevice * device )

把當前設備設為 device 。若已經有賦值設備, QTextStream 將調用 flush () 在替換舊設備前。

注意: 此函數把區域設置重置成 C 默認區域設置,並將編碼重置成 UTF-8 默認編碼。

另請參閱 device () 和 setString ().

void QTextStream:: setEncoding ( QStringConverter::Encoding encoding )

把用於此流的編碼設為 encoding 。編碼用於解碼從賦值設備讀取的任何數據,和用於編碼要寫入的任何數據。默認情況下, QStringConverter::Utf8 被使用,並啓用自動 Unicode 檢測。

QTextStream 運轉於字符串,此函數什麼都不做。

警告: 若調用此函數 (當文本流正從打開的順序套接字讀取時),內部緩衝可能仍包含使用舊編碼的解碼文本。

另請參閱 encoding (), setAutoDetectUnicode (),和 setLocale ().

void QTextStream:: setFieldAlignment ( QTextStream::FieldAlignment mode )

把字段對齊方式設為 mode 。當一起用於 setFieldWidth (),此函數允許生成對齊到左、對齊到右、或居中對齊的格式化輸齣文本。

另請參閱 fieldAlignment () 和 setFieldWidth ().

void QTextStream:: setFieldWidth ( int width )

把當前字段寬度設為 width 。若 width 為 0 (默認),字段寬度等於生成文本長度。

注意: 字段寬度適用於追加到此流的每個元素,在調用此函數後 (如:它還鋪墊 endl)。此行為不同於 STL (標準模闆庫) 類似類,在 STL,字段寬度隻適用於下一元素。

另請參閱 fieldWidth () 和 setPadChar ().

void QTextStream:: setGenerateByteOrderMark ( bool generate )

generate 為 true 並使用 UTF 編碼, QTextStream 將插入 BOM (字節序標記) 在把任何數據寫入設備前。若 generate 為 False,將不插入 BOM。必須在寫入任何數據之前,調用此函數。否則,什麼都不做。

另請參閱 generateByteOrderMark () 和 bom ().

void QTextStream:: setIntegerBase ( int base )

把整數的基設為 base ,用於讀取和生成數字兩者。 base 可以為 2 (二進製)、8 (八進製)、10 (十進製) 或 16 (十六進製)。若 base 為 0, QTextStream 將試圖檢測基通過審查流中的數據。當生成數字時, QTextStream 假定基為 10 除非有明確設置基。

另請參閱 integerBase (), QString::number (),和 setNumberFlags ().

void QTextStream:: setLocale (const QLocale & locale )

把用於此流的區域設置設為 locale 。指定的區域設置用於數字及其字符串錶示形式之間的轉換。

默認區域設置為 C 且是特殊情況 - 不使用韆位組分隔符,齣於嚮後兼容原因。

另請參閱 locale ().

void QTextStream:: setNumberFlags ( QTextStream::NumberFlags flags )

把當前數字標誌設為 flags . flags 是一組標誌來自 NumberFlag enum, and describes options for formatting generated code (e.g., whether or not to always write the base or sign of a number).

另請參閱 numberFlags (), setIntegerBase (),和 setRealNumberNotation ().

void QTextStream:: setPadChar ( QChar ch )

把鋪墊字符設為 ch 。默認值為 ASCII 空格字符 ' ',或 QChar (0x20)。此字符用於填充字段中的空格,當生成文本時。

範例:

QString s;
QTextStream out(&s);
out.setFieldWidth(10);
out.setFieldAlignment(QTextStream::AlignCenter);
out.setPadChar('-');
out << "Qt" << "rocks!";
					

字符串 s 包含:

----Qt------rocks!--
					

另請參閱 padChar () 和 setFieldWidth ().

void QTextStream:: setRealNumberNotation ( QTextStream::RealNumberNotation notation )

把實數錶示法設為 notation ( SmartNotation , FixedNotation , ScientificNotation )。當讀取並生成數字時, QTextStream 使用此值來檢測實數的格式。

另請參閱 realNumberNotation (), setRealNumberPrecision (), setNumberFlags (),和 setIntegerBase ().

void QTextStream:: setRealNumberPrecision ( int precision )

把實數的精度設為 precision . This value describes the number of fraction digits QTextStream should write when generating real numbers ( FixedNotation , ScientificNotation ),或最大數字的有效位數 ( SmartNotation ).

精度不可以為負值。默認值為 6。

另請參閱 realNumberPrecision () 和 setRealNumberNotation ().

void QTextStream:: setStatus ( QTextStream::Status status )

把文本流的狀態設為 status 給定。

忽略後續 setStatus() 調用,直到 resetStatus () 被調用。

另請參閱 Status , status (),和 resetStatus ().

void QTextStream:: setString ( QString * string , QIODeviceBase::OpenMode openMode = ReadWrite)

將當前字符串設為 string ,使用給定 openMode 。若已經有賦值設備, QTextStream 將調用 flush () 在替換它前。

另請參閱 string () 和 setDevice ().

void QTextStream:: skipWhiteSpace ()

Reads and discards whitespace from the stream until either a non-space character is detected, or until atEnd () returns true. This function is useful when reading a stream character by character.

空白字符是所有字符對於那些 QChar::isSpace () 返迴 true .

另請參閱 operator>> ().

QTextStream::Status QTextStream:: status () const

返迴文本流的狀態。

另請參閱 QTextStream::Status , setStatus (),和 resetStatus ().

QString *QTextStream:: string () const

返迴的當前字符串被賦值給 QTextStream ,或 nullptr 若沒有賦值字符串。

另請參閱 setString () 和 device ().

QTextStream &QTextStream:: operator<< ( QChar c )

寫入字符 c 到流,然後返迴引用針對 QTextStream .

另請參閱 setFieldWidth ().

QTextStream &QTextStream:: operator<< (const QString & string )

寫入字符串 string 到流,並返迴引用針對 QTextStream . The string is first encoded using the assigned encoding (the default is UTF-8) before it is written to the stream.

另請參閱 setFieldWidth () 和 setEncoding ().

QTextStream &QTextStream:: operator<< ( float f )

寫入實數 f 到流,然後返迴引用針對 QTextStream 。默認情況下, QTextStream 存儲它使用 SmartNotation , with up to 6 digits of precision. You can change the textual representation QTextStream will use for real numbers by calling setRealNumberNotation (), setRealNumberPrecision () 和 setNumberFlags ().

另請參閱 setFieldWidth (), setRealNumberNotation (), setRealNumberPrecision (),和 setNumberFlags ().

QTextStream &QTextStream:: operator<< ( short i )

寫入整數數字 i 到流,然後返迴引用針對 QTextStream . By default, the number is stored in decimal form, but you can also set the base by calling setIntegerBase ().

另請參閱 setFieldWidth () 和 setNumberFlags ().

QTextStream &QTextStream:: operator<< ( QLatin1StringView string )

這是重載函數。

寫入 string 到流,並返迴引用針對 QTextStream .

QTextStream &QTextStream:: operator<< ( QStringView string )

這是重載函數。

寫入 string 到流,並返迴引用針對 QTextStream .

QTextStream &QTextStream:: operator<< ( char c )

這是重載函數。

轉換 c 從 ASCII 到 QChar ,然後將它寫入流。

[since 6.3.1] QTextStream &QTextStream:: operator<< ( char16_t c )

這是重載函數。

寫入 Unicode 字符 c 到流,然後返迴引用針對 QTextStream .

該函數在 Qt 6.3.1 引入。

QTextStream &QTextStream:: operator<< (const QByteArray & array )

這是重載函數。

寫入 array 到流。內容對於 array 的轉換是采用 QString::fromUtf8 ().

QTextStream &QTextStream:: operator<< (const char * string )

這是重載函數。

寫入常量字符串指嚮通過 string 到流。 string is assumed to be in UTF-8 encoding. This operator is convenient when working with constant string data. Example:

QTextStream out(stdout);
out << "Qt rocks!" << Qt::endl;
					

警告: QTextStream assumes that string points to a string of text, terminated by a '\0' character. If there is no terminating '\0' character, your application may crash.

QTextStream &QTextStream:: operator<< (const void * ptr )

這是重載函數。

寫入 ptr 到流以具有基的十六進製數形式。

QTextStream &QTextStream:: operator<< ( double f )

這是重載函數。

寫入 double f 到流。

QTextStream &QTextStream:: operator<< ( int i )

這是重載函數。

寫入有符號整數 i 到流。

QTextStream &QTextStream:: operator<< ( long i )

這是重載函數。

寫入有符號 long i 到流。

QTextStream &QTextStream:: operator<< ( qlonglong i )

這是重載函數。

寫入 qlonglong i 到流。

QTextStream &QTextStream:: operator<< ( qulonglong i )

這是重載函數。

寫入 qulonglong i 到流。

QTextStream &QTextStream:: operator<< ( unsigned int i )

這是重載函數。

寫入無符號整數 i 到流。

QTextStream &QTextStream:: operator<< ( unsigned long i )

這是重載函數。

寫入無符號 long i 到流。

QTextStream &QTextStream:: operator<< ( unsigned short i )

這是重載函數。

寫入無符號短整數 i 到流。

QTextStream &QTextStream:: operator>> ( QChar & c )

從流讀取字符並把它存儲在 c . Returns a reference to the QTextStream , so several operators can be nested. Example:

QTextStream in(file);
QChar ch1, ch2, ch3;
in >> ch1 >> ch2 >> ch3;
					

空白 not 跳過。

QTextStream &QTextStream:: operator>> ( QString & str )

Reads a word from the stream and stores it in str , then returns a reference to the stream. Words are separated by whitespace (i.e., all characters for which QChar::isSpace () 返迴 true ).

跳過前導空白。

QTextStream &QTextStream:: operator>> ( float & f )

Reads a real number from the stream and stores it in f ,然後返迴引用針對 QTextStream . The number is cast to the correct type. If no real number is detect on the stream, f is set to 0.0.

As a special exception, QTextStream allows the strings "nan" and "inf" to represent NAN and INF floats or doubles.

跳過前導空白。

QTextStream &QTextStream:: operator>> ( short & i )

讀取整數從流並把它存儲在 i ,然後返迴引用針對 QTextStream 。把數字鑄造成正確類型,在存儲它之前。若在流中未檢測到數字, i 被設為 0。

默認情況下, QTextStream 將試圖使用下列規則以檢測數字的基:

前綴
0b 或 0B 2 (二進製)
"0" followed by "0-7" 8 (八進製)
"0" otherwise 10 (十進製)
0x 或 0X 16 (十六進製)
1-9 10 (十進製)

通過調用 setIntegerBase (), you can specify the integer base explicitly. This will disable the auto-detection, and speed up QTextStream slightly.

跳過前導空白。

QTextStream &QTextStream:: operator>> ( QByteArray & array )

這是重載函數。

把單詞轉換成 UTF-8,然後把它存儲在 array .

另請參閱 QString::toLatin1 ().

QTextStream &QTextStream:: operator>> ( char & c )

這是重載函數。

從流讀取字符並把它存儲在 c . The character from the stream is converted to ISO-8859-1 before it is stored.

另請參閱 QChar::toLatin1 ().

QTextStream &QTextStream:: operator>> ( char * c )

這是重載函數。

Converts the word to UTF-8 and stores it in c , terminated by a '\0' character. If no word is available, only the '\0' character is stored.

Warning: Although convenient, this operator is dangerous and must be used with care. QTextStream assumes that c points to a buffer with enough space to hold the word. If the buffer is too small, your application may crash. For a word consisting of n QChars, the buffer needs to be at least 3*n+1 characters long.

若可能的話,使用 QByteArray 操作符代替。

[since 6.4] QTextStream &QTextStream:: operator>> ( char16_t & c )

這是重載函數。

從流讀取字符並把它存儲在 c .

該函數在 Qt 6.4 引入。

QTextStream &QTextStream:: operator>> ( double & f )

這是重載函數。

把實數存儲在 double f .

QTextStream &QTextStream:: operator>> ( int & i )

這是重載函數。

Stores the integer in the signed int i .

QTextStream &QTextStream:: operator>> ( long & i )

這是重載函數。

Stores the integer in the signed long i .

QTextStream &QTextStream:: operator>> ( qlonglong & i )

這是重載函數。

Stores the integer in the qlonglong i .

QTextStream &QTextStream:: operator>> ( qulonglong & i )

這是重載函數。

Stores the integer in the qulonglong i .

QTextStream &QTextStream:: operator>> ( unsigned int & i )

這是重載函數。

Stores the integer in the unsigned int i .

QTextStream &QTextStream:: operator>> ( unsigned long & i )

這是重載函數。

Stores the integer in the unsigned long i .

QTextStream &QTextStream:: operator>> ( unsigned short & i )

這是重載函數。

Stores the integer in the unsigned short i .

相關非成員

QTextStreamManipulator qSetFieldWidth ( int width )

相當於 QTextStream::setFieldWidth ( width ).

QTextStreamManipulator qSetPadChar ( QChar ch )

相當於 QTextStream::setPadChar ( ch ).

QTextStreamManipulator qSetRealNumberPrecision ( int precision )

相當於 QTextStream::setRealNumberPrecision ( precision ).