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 (QString * string , QIODeviceBase::OpenMode openMode = ReadWrite)
QTextStream (QByteArray * array , 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<< (char c )
QTextStream & operator<< (char16_t c )
QTextStream & operator<< (short i )
QTextStream & operator<< (unsigned short i )
QTextStream & operator<< (int i )
QTextStream & operator<< (unsigned int i )
QTextStream & operator<< (long i )
QTextStream & operator<< (unsigned long i )
QTextStream & operator<< (qlonglong i )
QTextStream & operator<< (qulonglong i )
QTextStream & operator<< (float f )
QTextStream & operator<< (double f )
QTextStream & operator<< (const QString & string )
QTextStream & operator<< (QStringView string )
QTextStream & operator<< (QLatin1StringView string )
QTextStream & operator<< (const QByteArray & array )
QTextStream & operator<< (const char * string )
QTextStream & operator<< (const void * ptr )
QTextStream & operator>> (QChar & c )
QTextStream & operator>> (char & c )
QTextStream & operator>> (char16_t & c )
QTextStream & operator>> (short & i )
QTextStream & operator>> (unsigned short & i )
QTextStream & operator>> (int & i )
QTextStream & operator>> (unsigned int & i )
QTextStream & operator>> (long & i )
QTextStream & operator>> (unsigned long & i )
QTextStream & operator>> (qlonglong & i )
QTextStream & operator>> (qulonglong & i )
QTextStream & operator>> (float & f )
QTextStream & operator>> (double & f )
QTextStream & operator>> (QString & str )
QTextStream & operator>> (QByteArray & array )
QTextStream & operator>> (char * c )
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       "
}
					

It's also common to use QTextStream to read console input and write console output. QTextStream is locale aware, and will automatically decode standard input using the correct encoding. Example:

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

Besides using QTextStream's constructors, you can also set the device or string QTextStream operates on by calling setDevice () 或 setString (). You can seek to a position by calling seek (),和 atEnd () will return true when there is no data left to be read. If you call flush (), QTextStream will empty all data from its write buffer into the device and call flush () on the device.

Internally, QTextStream uses a Unicode based buffer, and QStringConverter is used by QTextStream to automatically support different encodings. By default, UTF-8 is used for reading and writing, but you can also set the encoding by calling setEncoding (). Automatic Unicode detection is also supported. When this feature is enabled (the default behavior), QTextStream will detect the UTF-8, UTF-16 or the UTF-32 BOM (Byte Order Mark) and switch to the appropriate UTF encoding when reading. QTextStream does not write a BOM by default, but you can enable this by calling setGenerateByteOrderMark (true). When QTextStream operates on a QString directly, the encoding is disabled.

There are three general ways to use QTextStream when reading text files:

  • Chunk by chunk, by calling readLine () 或 readAll ().
  • Word by word. QTextStream supports streaming into QString s, QByteArray s and char* buffers. Words are delimited by space, and leading white space is automatically skipped.
  • Character by character, by streaming into QChar or char types. This method is often used for convenient input handling when parsing files, independent of character encoding and end-of-line semantics. To skip white space, call skipWhiteSpace ().

Since the text stream uses a buffer, you should not read from the stream using the implementation of a superclass. For instance, if you have a QFile and read from it directly using QFile::readLine () instead of using the stream, the text stream's internal position will be out of sync with the file's position.

By default, when reading numbers from a stream of text, QTextStream will automatically detect the number's base representation. For example, if the number starts with "0x", it is assumed to be in hexadecimal form. If it starts with the digits 1-9, it is assumed to be in decimal form, and so on. You can set the integer base, thereby disabling the automatic detection, by calling setIntegerBase ()。范例:

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

QTextStream supports many formatting options for generating text. You can set the field width and pad character by calling setFieldWidth () 和 setPadChar ()。使用 setFieldAlignment () to set the alignment within each field. For real numbers, call setRealNumberNotation () 和 setRealNumberPrecision () to set the notation ( SmartNotation , ScientificNotation , FixedNotation ) and precision in digits of the generated number. Some extra number formatting options are also available through 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 Same as operator<<('\n') and flush ().
Qt::flush 如同 flush ().
Qt::reset 如同 reset ().
Qt::ws 如同 skipWhiteSpace ().
Qt::bom 如同 setGenerateByteOrderMark (true)。

In addition, Qt provides three global manipulators that take a parameter: qSetFieldWidth (), qSetPadChar (),和 qSetRealNumberPrecision ().

另请参阅 QDataStream , QIODevice , QFile , QBuffer ,和 QTcpSocket .

成员类型文档编制

enum QTextStream:: FieldAlignment

This enum specifies how to align text in fields when the field is wider than the text that occupies it.

常量 描述
QTextStream::AlignLeft 0 铺垫在字段右侧。
QTextStream::AlignRight 1 铺垫在字段左侧。
QTextStream::AlignCenter 2 铺垫在字段的两侧。
QTextStream::AlignAccountingStyle 3 Same as AlignRight, except that the sign of a number is flush left.

另请参阅 setFieldAlignment ().

enum QTextStream:: NumberFlag
flags QTextStream:: NumberFlags

This enum specifies various flags that can be set to affect the output of integers, float s, and double s.

常量 描述
QTextStream::ShowBase 0x1 Show the base as a prefix if the base is 16 ("0x"), 8 ("0"), or 2 ("0b").
QTextStream::ForcePoint 0x2 Always put the decimal separator in numbers, even if there are no decimals.
QTextStream::ForceSign 0x4 Always put the sign in numbers, even for positive numbers.
QTextStream::UppercaseBase 0x8 Use uppercase versions of base prefixes ("0X", "0B").
QTextStream::UppercaseDigits 0x10 Use uppercase letters for expressing digits 10 to 35 instead of lowercase.

The NumberFlags type is a typedef for QFlags <NumberFlag>. It stores an OR combination of NumberFlag values.

另请参阅 setNumberFlags ().

enum QTextStream:: RealNumberNotation

This enum specifies which notations to use for expressing float and double as strings.

常量 描述
QTextStream::ScientificNotation 2 科学表示法 ( printf() 's %e 标志)。
QTextStream::FixedNotation 1 定点表示法 ( printf() 's %f 标志)。
QTextStream::SmartNotation 0 Scientific or fixed-point notation, depending on which makes most sense ( printf() 's %g 标志)。

另请参阅 setRealNumberNotation ().

enum QTextStream:: Status

此枚举描述文本流的当前状态。

常量 描述
QTextStream::Ok 0 文本流运转正常。
QTextStream::ReadPastEnd 1 The text stream has read past the end of the data in the underlying device.
QTextStream::ReadCorruptData 2 文本流有读取被破坏的数据。
QTextStream::WriteFailed 3 The text stream cannot write to the underlying device.

另请参阅 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 ( QString * string , QIODeviceBase::OpenMode openMode = ReadWrite)

构造 QTextStream 运转于 string ,使用 openMode 定义打开模式。

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

构造 QTextStream 运转于 array ,使用 openMode to define the open mode. Internally, the array is wrapped by a QBuffer .

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

构造 QTextStream 运转于 array ,使用 openMode to define the open mode. The array is accessed as read-only, regardless of the values in openMode .

This constructor is convenient for working on constant strings. Example:

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 if there is no more data to be read from the QTextStream ;否则返回 false . This is similar to, but not the same as calling QIODevice::atEnd (), as QTextStream also takes into account its internal Unicode buffer.

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 is set to generate the UTF BOM (Byte Order Mark) when using a UTF encoding; otherwise returns false . UTF BOM generation is set to false by default.

另请参阅 setGenerateByteOrderMark ().

int QTextStream:: integerBase () const

Returns the current base of integers. 0 means that the base is detected when reading, or 10 (decimal) when generating numbers.

另请参阅 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

Returns the device position corresponding to the current position of the stream, or -1 if an error occurs (e.g., if there is no device or string, or if there's a device error).

因为 QTextStream is buffered, this function may have to seek the device to reconstruct a valid device position. This operation can be expensive, so you may want to avoid calling this function in a tight loop.

另请参阅 seek ().

QString QTextStream:: read ( qint64 maxlen )

读取最多 maxlen characters from the stream, and returns the data read as a QString .

另请参阅 readAll (), readLine (),和 QIODevice::read ().

QString QTextStream:: readAll ()

读取流的整个内容,并返回它按 QString . Avoid this function when working on large files, as it will consume a significant amount of memory.

调用 readLine () is better if you do not know how much data is available.

另请参阅 readLine ().

QString QTextStream:: readLine ( qint64 maxlen = 0)

Reads one line of text from the stream, and returns it as a QString . The maximum allowed line length is set to maxlen . If the stream contains lines longer than this, then the lines will be split after maxlen characters and returned in parts.

maxlen 为 0,行可以是任意长度。

The returned line has no trailing end-of-line characters ("\n" or "\r\n"), so calling QString::trimmed () can be unnecessary.

If the stream has read to the end of the file, readLine() will return a null QString . For strings, or for devices that support it, you can explicitly test for the end of the stream using atEnd ().

另请参阅 readAll () 和 QIODevice::readLine ().

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

Reads one line of text from the stream into line 。若 line is nullptr , the read line is not stored.

The maximum allowed line length is set to maxlen . If the stream contains lines longer than this, then the lines will be split after maxlen characters and returned in parts.

maxlen 为 0,行可以是任意长度。

The resulting line has no trailing end-of-line characters ("\n" or "\r\n"), so calling QString::trimmed () can be unnecessary.

line has sufficient capacity for the data that is about to be read, this function may not need to allocate new memory. Because of this, it can be faster than readLine ().

返回 false if the stream has read to the end of the file or an error has occurred; otherwise returns true . The contents in line before the call are discarded in any case.

另请参阅 readAll () 和 QIODevice::readLine ().

QTextStream::RealNumberNotation QTextStream:: realNumberNotation () const

返回当前实数表示法。

另请参阅 setRealNumberNotation (), realNumberPrecision (), numberFlags (),和 integerBase ().

int QTextStream:: realNumberPrecision () const

Returns the current real number precision, or the number of fraction digits QTextStream will write when generating real numbers ( FixedNotation , ScientificNotation ), or the maximum number of significant digits ( SmartNotation ).

另请参阅 setRealNumberPrecision (), setRealNumberNotation (), realNumberNotation (), numberFlags (),和 integerBase ().

void QTextStream:: reset ()

Resets QTextStream 's formatting options, bringing it back to its original constructed state. The device, string and any buffered data is left untouched.

void QTextStream:: resetStatus ()

Resets the status of the text stream.

另请参阅 QTextStream::Status , status (),和 setStatus ().

bool QTextStream:: seek ( qint64 pos )

Seeks to the position pos in the device. Returns true 当成功时;否则返回 false .

void QTextStream:: setAutoDetectUnicode ( bool enabled )

enabled 为 True, QTextStream will attempt to detect Unicode encoding by peeking into the stream data to see if it can find the UTF-8, UTF-16, or UTF-32 Byte Order Mark (BOM). If this mark is found, QTextStream will replace the current encoding with the UTF encoding.

This function can be used together with setEncoding (). It is common to set the encoding to UTF-8, and then enable UTF-16 detection.

另请参阅 autoDetectUnicode () 和 setEncoding ().

void QTextStream:: setDevice ( QIODevice * device )

把当前设备设为 device . If a device has already been assigned, QTextStream will call flush () before the old device is replaced.

注意: This function resets locale to the default locale ('C') and encoding to the default encoding, UTF-8.

另请参阅 device () 和 setString ().

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

Sets the encoding for this stream to encoding . The encoding is used for decoding any data that is read from the assigned device, and for encoding any data that is written. By default, QStringConverter::Utf8 is used, and automatic unicode detection is enabled.

QTextStream 运转于字符串,此函数什么都不做。

警告: If you call this function while the text stream is reading from an open sequential socket, the internal buffer may still contain text decoded using the old encoding.

另请参阅 encoding (), setAutoDetectUnicode (),和 setLocale ().

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

Sets the field alignment to mode . When used together with setFieldWidth (), this function allows you to generate formatted output with text aligned to the left, to the right or center aligned.

另请参阅 fieldAlignment () 和 setFieldWidth ().

void QTextStream:: setFieldWidth ( int width )

Sets the current field width to width 。若 width is 0 (the default), the field width is equal to the length of the generated text.

注意: The field width applies to every element appended to this stream after this function has been called (e.g., it also pads endl). This behavior is different from similar classes in the STL, where the field width only applies to the next element.

另请参阅 fieldWidth () 和 setPadChar ().

void QTextStream:: setGenerateByteOrderMark ( bool generate )

generate is true and a UTF encoding is used, QTextStream will insert the BOM (Byte Order Mark) before any data has been written to the device. If generate is false, no BOM will be inserted. This function must be called before any data is written. Otherwise, it does nothing.

另请参阅 generateByteOrderMark () 和 bom ().

void QTextStream:: setIntegerBase ( int base )

Sets the base of integers to base , both for reading and for generating numbers. base can be either 2 (binary), 8 (octal), 10 (decimal) or 16 (hexadecimal). If base is 0, QTextStream will attempt to detect the base by inspecting the data on the stream. When generating numbers, QTextStream assumes base is 10 unless the base has been set explicitly.

另请参阅 integerBase (), QString::number (),和 setNumberFlags ().

void QTextStream:: setLocale (const QLocale & locale )

Sets the locale for this stream to locale . The specified locale is used for conversions between numbers and their string representations.

The default locale is C and it is a special case - the thousands group separator is not used for backward compatibility reasons.

另请参阅 locale ().

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

Sets the current number flags to flags . flags is a set of flags from the 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 )

Sets the pad character to ch . The default value is the ASCII space character (' '), or QChar (0x20). This character is used to fill in the space in fields when generating text.

范例:

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

字符串 s contains:

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

另请参阅 padChar () 和 setFieldWidth ().

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

Sets the real number notation to notation ( SmartNotation , FixedNotation , ScientificNotation ). When reading and generating numbers, QTextStream uses this value to detect the formatting of real numbers.

另请参阅 realNumberNotation (), setRealNumberPrecision (), setNumberFlags (),和 setIntegerBase ().

void QTextStream:: setRealNumberPrecision ( int precision )

Sets the precision of real numbers to precision . This value describes the number of fraction digits QTextStream should write when generating real numbers ( FixedNotation , ScientificNotation ), or the maximum number of significant digits ( SmartNotation ).

The precision cannot be a negative value. The default value is 6.

另请参阅 realNumberPrecision () 和 setRealNumberNotation ().

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

Sets the status of the text stream to the status 给定。

Subsequent calls to setStatus() are ignored until resetStatus () 被调用。

另请参阅 Status , status (),和 resetStatus ().

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

将当前字符串设为 string ,使用给定 openMode . If a device has already been assigned, QTextStream will call flush () before replacing it.

另请参阅 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.

Whitespace characters are all characters for which QChar::isSpace () 返回 true .

另请参阅 operator>> ().

QTextStream::Status QTextStream:: status () const

返回文本流的状态。

另请参阅 QTextStream::Status , setStatus (),和 resetStatus ().

QString *QTextStream:: string () const

Returns the current string assigned to the QTextStream ,或 nullptr if no string has been assigned.

另请参阅 setString () 和 device ().

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

写入字符 c to the stream, then returns a reference to the QTextStream .

另请参阅 setFieldWidth ().

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

这是重载函数。

转换 c 从 ASCII 到 QChar ,然后将它写入流。

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

这是重载函数。

Writes the Unicode character c to the stream, then returns a reference to the QTextStream .

该函数在 Qt 6.3.1 引入。

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

Writes the integer number i to the stream, then returns a reference to the 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<< ( unsigned short i )

这是重载函数。

写入无符号短整数 i 到流。

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

这是重载函数。

写入有符号整数 i 到流。

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

这是重载函数。

写入无符号整数 i 到流。

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

这是重载函数。

Writes the signed long i 到流。

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

这是重载函数。

Writes the unsigned long i 到流。

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

这是重载函数。

Writes the qlonglong i 到流。

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

这是重载函数。

Writes the qulonglong i 到流。

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

Writes the real number f to the stream, then returns a reference to the QTextStream . By default, QTextStream stores it using 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<< ( double f )

这是重载函数。

Writes the double f 到流。

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

Writes the string string to the stream, and returns a reference to the 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<< ( QStringView string )

这是重载函数。

写入 string to the stream, and returns a reference to the QTextStream .

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

这是重载函数。

写入 string to the stream, and returns a reference to the QTextStream .

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

这是重载函数。

写入 array to the stream. The contents of array are converted with QString::fromUtf8 ().

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

这是重载函数。

Writes the constant string pointed to by 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 to the stream as a hexadecimal number with a base.

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

Reads a character from the stream and stores it in 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;
					

Whitespace is not skipped.

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

这是重载函数。

Reads a character from the stream and stores it in c . The character from the stream is converted to ISO-8859-1 before it is stored.

另请参阅 QChar::toLatin1 ().

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

这是重载函数。

Reads a character from the stream and stores it in c .

该函数在 Qt 6.4 引入。

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

Reads an integer from the stream and stores it in i , then returns a reference to the QTextStream . The number is cast to the correct type before it is stored. If no number was detected on the stream, i is set to 0.

默认情况下, QTextStream will attempt to detect the base of the number using the following rules:

前缀
"0b" or "0B" 2 (二进制)
"0" followed by "0-7" 8 (八进制)
"0" otherwise 10 (十进制)
"0x" or "0X" 16 (十六进制)
"1" to "9" 10 (十进制)

By calling setIntegerBase (), you can specify the integer base explicitly. This will disable the auto-detection, and speed up QTextStream slightly.

Leading whitespace is skipped.

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

这是重载函数。

Stores the integer in the unsigned short i .

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

这是重载函数。

Stores the integer in the signed int i .

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

这是重载函数。

Stores the integer in the unsigned int i .

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

这是重载函数。

Stores the integer in the signed long i .

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

这是重载函数。

Stores the integer in the unsigned 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>> ( float & f )

Reads a real number from the stream and stores it in f , then returns a reference to the 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.

Leading whitespace is skipped.

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

这是重载函数。

Stores the real number in the double f .

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

Leading whitespace is skipped.

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

这是重载函数。

Converts the word to UTF-8, then stores it in array .

另请参阅 QString::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.

If possible, use the QByteArray operator instead.

相关非成员

QTextStreamManipulator qSetFieldWidth ( int width )

相当于 QTextStream::setFieldWidth ( width ).

QTextStreamManipulator qSetPadChar ( QChar ch )

相当于 QTextStream::setPadChar ( ch ).

QTextStreamManipulator qSetRealNumberPrecision ( int precision )

相当于 QTextStream::setRealNumberPrecision ( precision ).