QtBase  v6.3.1
Public Types | Public Member Functions | Friends | Related Functions | List of all members
QTextStream Class Reference

The QTextStream class provides a convenient interface for reading and writing text. More...

#include <qtextstream.h>

Inheritance diagram for QTextStream:
Inheritance graph
[legend]
Collaboration diagram for QTextStream:
Collaboration graph
[legend]

Public Types

enum  RealNumberNotation { SmartNotation , FixedNotation , ScientificNotation }
 
enum  FieldAlignment { AlignLeft , AlignRight , AlignCenter , AlignAccountingStyle }
 
enum  Status { Ok , ReadPastEnd , ReadCorruptData , WriteFailed }
 
enum  NumberFlag {
  ShowBase = 0x1 , ForcePoint = 0x2 , ForceSign = 0x4 , UppercaseBase = 0x8 ,
  UppercaseDigits = 0x10
}
 
- Public Types inherited from QIODeviceBase
enum  OpenModeFlag {
  NotOpen = 0x0000 , ReadOnly = 0x0001 , WriteOnly = 0x0002 , ReadWrite = ReadOnly | WriteOnly ,
  Append = 0x0004 , Truncate = 0x0008 , Text = 0x0010 , Unbuffered = 0x0020 ,
  NewOnly = 0x0040 , ExistingOnly = 0x0080
}
 

Public Member Functions

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

Friends

class QDebugStateSaverPrivate
 
class QDebug
 

Related Functions

(Note that these are not member functions.)

QTextStreamManipulator qSetFieldWidth (int width)
 
QTextStreamManipulator qSetPadChar (QChar ch)
 
QTextStreamManipulator qSetRealNumberPrecision (int precision)
 

Additional Inherited Members

- Protected Member Functions inherited from QIODeviceBase
 ~QIODeviceBase ()=default
 

Detailed Description

The QTextStream class provides a convenient interface for reading and writing text.

\inmodule QtCore

\reentrant

QTextStream can operate on a QIODevice, a QByteArray or a QString. Using QTextStream's streaming operators, you can conveniently read and write words, lines and numbers. For generating text, QTextStream supports formatting options for field padding and alignment, and formatting of numbers. Example:

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:

Besides using QTextStream's constructors, you can also set the device or string QTextStream operates on by calling setDevice() or setString(). You can seek to a position by calling seek(), and 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:

\list

\endlist

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(). Example:

QTextStream supports many formatting options for generating text. You can set the field width and pad character by calling setFieldWidth() and setPadChar(). Use setFieldAlignment() to set the alignment within each field. For real numbers, call setRealNumberNotation() and 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().

\target QTextStream manipulators

Like <iostream> in the standard C++ library, QTextStream also defines several global manipulator functions:

\table \header

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

See also
QDataStream, QIODevice, QFile, QBuffer, QTcpSocket

Definition at line 61 of file qtextstream.h.

Member Enumeration Documentation

◆ FieldAlignment

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

\value AlignLeft Pad on the right side of fields. \value AlignRight Pad on the left side of fields. \value AlignCenter Pad on both sides of field. \value AlignAccountingStyle Same as AlignRight, except that the sign of a number is flush left.

See also
setFieldAlignment()
Enumerator
AlignLeft 
AlignRight 
AlignCenter 
AlignAccountingStyle 

Definition at line 71 of file qtextstream.h.

◆ NumberFlag

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

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

See also
setNumberFlags()
Enumerator
ShowBase 
ForcePoint 
ForceSign 
UppercaseBase 
UppercaseDigits 

Definition at line 83 of file qtextstream.h.

◆ RealNumberNotation

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

\value ScientificNotation Scientific notation ({printf()}'s e flag). \value FixedNotation Fixed-point notation ({printf()}'s f flag). \value SmartNotation Scientific or fixed-point notation, depending on which makes most sense ({printf()}'s g flag).

See also
setRealNumberNotation()
Enumerator
SmartNotation 
FixedNotation 
ScientificNotation 

Definition at line 66 of file qtextstream.h.

◆ Status

This enum describes the current status of the text stream.

\value Ok The text stream is operating normally. \value ReadPastEnd The text stream has read past the end of the data in the underlying device. \value ReadCorruptData The text stream has read corrupt data. \value WriteFailed The text stream cannot write to the underlying device.

See also
status()
Enumerator
Ok 
ReadPastEnd 
ReadCorruptData 
WriteFailed 

Definition at line 77 of file qtextstream.h.

Constructor & Destructor Documentation

◆ QTextStream() [1/6]

QTextStream::QTextStream ( )

Constructs a QTextStream. Before you can use it for reading or writing, you must assign a device or a string.

See also
setDevice(), setString()

Definition at line 917 of file qtextstream.cpp.

◆ QTextStream() [2/6]

QTextStream::QTextStream ( QIODevice device)
explicit

Constructs a QTextStream that operates on device.

Definition at line 930 of file qtextstream.cpp.

Here is the call graph for this function:

◆ QTextStream() [3/6]

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

Constructs a QTextStream that operates on fileHandle, using openMode to define the open mode. Internally, a QFile is created to handle the FILE pointer.

This constructor is useful for working directly with the common FILE based input and output streams: stdin, stdout and stderr. Example:

Definition at line 1025 of file qtextstream.cpp.

Here is the call graph for this function:

◆ QTextStream() [4/6]

QTextStream::QTextStream ( QString string,
OpenMode  openMode = ReadWrite 
)
explicit

Constructs a QTextStream that operates on string, using openMode to define the open mode.

Definition at line 949 of file qtextstream.cpp.

Here is the call graph for this function:

◆ QTextStream() [5/6]

QTextStream::QTextStream ( QByteArray array,
OpenMode  openMode = ReadWrite 
)
explicit

Constructs a QTextStream that operates on array, using openMode to define the open mode. Internally, the array is wrapped by a QBuffer.

Definition at line 967 of file qtextstream.cpp.

◆ QTextStream() [6/6]

QTextStream::QTextStream ( const QByteArray array,
OpenMode  openMode = ReadOnly 
)
explicit

Constructs a QTextStream that operates on array, using 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:

Definition at line 994 of file qtextstream.cpp.

Here is the call graph for this function:

◆ ~QTextStream()

QTextStream::~QTextStream ( )
virtual

Destroys the QTextStream.

If the stream operates on a device, flush() will be called implicitly. Otherwise, the device is unaffected.

Definition at line 1050 of file qtextstream.cpp.

Member Function Documentation

◆ atEnd()

bool QTextStream::atEnd ( ) const

Returns true if there is no more data to be read from the QTextStream; otherwise returns false. This is similar to, but not the same as calling QIODevice::atEnd(), as QTextStream also takes into account its internal Unicode buffer.

Definition at line 1513 of file qtextstream.cpp.

Here is the caller graph for this function:

◆ autoDetectUnicode()

bool QTextStream::autoDetectUnicode ( ) const

Returns true if automatic Unicode detection is enabled, otherwise returns false. Automatic Unicode detection is enabled by default.

See also
setAutoDetectUnicode(), setEncoding()

Definition at line 3012 of file qtextstream.cpp.

◆ device()

QIODevice * QTextStream::device ( ) const

Returns the current device associated with the QTextStream, or \nullptr if no device has been assigned.

See also
setDevice(), string()

Definition at line 1225 of file qtextstream.cpp.

Here is the caller graph for this function:

◆ encoding()

QStringConverter::Encoding QTextStream::encoding ( ) const

Returns the encoding that is current assigned to the stream.

See also
setEncoding(), setAutoDetectUnicode(), locale()

Definition at line 2983 of file qtextstream.cpp.

◆ fieldAlignment()

QTextStream::FieldAlignment QTextStream::fieldAlignment ( ) const

Returns the current field alignment.

See also
setFieldAlignment(), fieldWidth()

Definition at line 1288 of file qtextstream.cpp.

◆ fieldWidth()

int QTextStream::fieldWidth ( ) const

Returns the current field width.

See also
setFieldWidth()

Definition at line 1349 of file qtextstream.cpp.

◆ flush()

void QTextStream::flush ( )

Flushes any buffered data waiting to be written to the device.

If QTextStream operates on a string, this function does nothing.

Definition at line 1077 of file qtextstream.cpp.

Here is the caller graph for this function:

◆ generateByteOrderMark()

bool QTextStream::generateByteOrderMark ( ) const

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

See also
setGenerateByteOrderMark()

Definition at line 3043 of file qtextstream.cpp.

◆ integerBase()

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.

See also
setIntegerBase(), QString::number(), numberFlags()

Definition at line 1402 of file qtextstream.cpp.

◆ locale()

QLocale QTextStream::locale ( ) const
Since
4.5

Returns the locale for this stream. The default locale is C.

See also
setLocale()

Definition at line 3073 of file qtextstream.cpp.

Here is the caller graph for this function:

◆ numberFlags()

QTextStream::NumberFlags QTextStream::numberFlags ( ) const

Returns the current number flags.

See also
setNumberFlags(), integerBase(), realNumberNotation()

Definition at line 1374 of file qtextstream.cpp.

Here is the caller graph for this function:

◆ operator<<() [1/19]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Converts c from ASCII to a QChar, then writes it to the stream.

Definition at line 2280 of file qtextstream.cpp.

Here is the call graph for this function:

◆ operator<<() [2/19]

QTextStream & QTextStream::operator<< ( char16_t  c)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Since
6.3.1

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

Definition at line 172 of file qtextstream.h.

◆ operator<<() [3/19]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Writes the constant string pointed to by string to the stream. string is assumed to be in UTF-8 encoding. This operator is convenient when working with constant string data. Example:

Warning: 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.

Definition at line 2543 of file qtextstream.cpp.

◆ operator<<() [4/19]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

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

Definition at line 2522 of file qtextstream.cpp.

Here is the call graph for this function:

◆ operator<<() [5/19]

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.

See also
setFieldWidth(), setEncoding()

Definition at line 2479 of file qtextstream.cpp.

◆ operator<<() [6/19]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Writes ptr to the stream as a hexadecimal number with a base.

Definition at line 2557 of file qtextstream.cpp.

◆ operator<<() [7/19]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Writes the double f to the stream.

Definition at line 2425 of file qtextstream.cpp.

Here is the call graph for this function:

◆ operator<<() [8/19]

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() and setNumberFlags().

See also
setFieldWidth(), setRealNumberNotation(), setRealNumberPrecision(), setNumberFlags()

Definition at line 2415 of file qtextstream.cpp.

◆ operator<<() [9/19]

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

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

See also
setFieldWidth()

Definition at line 2267 of file qtextstream.cpp.

◆ operator<<() [10/19]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Writes string to the stream, and returns a reference to the QTextStream.

Definition at line 2508 of file qtextstream.cpp.

◆ operator<<() [11/19]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Writes the qlonglong i to the stream.

Definition at line 2383 of file qtextstream.cpp.

Here is the call graph for this function:

◆ operator<<() [12/19]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Writes string to the stream, and returns a reference to the QTextStream.

Since
5.12

Definition at line 2494 of file qtextstream.cpp.

Here is the call graph for this function:

◆ operator<<() [13/19]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Writes the qulonglong i to the stream.

Definition at line 2396 of file qtextstream.cpp.

◆ operator<<() [14/19]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Writes the signed int i to the stream.

Definition at line 2331 of file qtextstream.cpp.

Here is the call graph for this function:

◆ operator<<() [15/19]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Writes the signed long i to the stream.

Definition at line 2357 of file qtextstream.cpp.

Here is the call graph for this function:

◆ operator<<() [16/19]

QTextStream & QTextStream::operator<< ( signed 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().

See also
setFieldWidth(), setNumberFlags()

Definition at line 2305 of file qtextstream.cpp.

Here is the call graph for this function:

◆ operator<<() [17/19]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Writes the unsigned int i to the stream.

Definition at line 2344 of file qtextstream.cpp.

◆ operator<<() [18/19]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Writes the unsigned long i to the stream.

Definition at line 2370 of file qtextstream.cpp.

◆ operator<<() [19/19]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Writes the unsigned short i to the stream.

Definition at line 2318 of file qtextstream.cpp.

◆ operator>>() [1/15]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

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

See also
QChar::toLatin1()

Definition at line 1988 of file qtextstream.cpp.

◆ operator>>() [2/15]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

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.

Definition at line 2192 of file qtextstream.cpp.

◆ operator>>() [3/15]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Stores the real number in the double f.

Definition at line 2116 of file qtextstream.cpp.

◆ operator>>() [4/15]

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.

Definition at line 2106 of file qtextstream.cpp.

◆ operator>>() [5/15]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

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

See also
QString::toLatin1()

Definition at line 2156 of file qtextstream.cpp.

◆ operator>>() [6/15]

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:

Whitespace is not skipped.

Definition at line 1969 of file qtextstream.cpp.

Here is the call graph for this function:

◆ operator>>() [7/15]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Stores the integer in the qlonglong i.

Definition at line 2080 of file qtextstream.cpp.

◆ operator>>() [8/15]

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() returns true).

Leading whitespace is skipped.

Definition at line 2128 of file qtextstream.cpp.

Here is the call graph for this function:

◆ operator>>() [9/15]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Stores the integer in the qulonglong i.

Definition at line 2090 of file qtextstream.cpp.

◆ operator>>() [10/15]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Stores the integer in the signed int i.

Definition at line 2040 of file qtextstream.cpp.

◆ operator>>() [11/15]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Stores the integer in the signed long i.

Definition at line 2060 of file qtextstream.cpp.

◆ operator>>() [12/15]

QTextStream & QTextStream::operator>> ( signed 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.

By default, QTextStream will attempt to detect the base of the number using the following rules:

\table \header

  • Prefix
  • Base \row
  • "0b" or "0B"
  • 2 (binary) \row
  • "0" followed by "0-7"
  • 8 (octal) \row
  • "0" otherwise
  • 10 (decimal) \row
  • "0x" or "0X"
  • 16 (hexadecimal) \row
  • "1" to "9"
  • 10 (decimal) \endtable

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.

Definition at line 2020 of file qtextstream.cpp.

◆ operator>>() [13/15]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Stores the integer in the unsigned int i.

Definition at line 2050 of file qtextstream.cpp.

◆ operator>>() [14/15]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Stores the integer in the unsigned long i.

Definition at line 2070 of file qtextstream.cpp.

◆ operator>>() [15/15]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Stores the integer in the unsigned short i.

Definition at line 2030 of file qtextstream.cpp.

◆ padChar()

QChar QTextStream::padChar ( ) const

Returns the current pad character.

See also
setPadChar(), setFieldWidth()

Definition at line 1320 of file qtextstream.cpp.

◆ pos()

qint64 QTextStream::pos ( ) const
Since
4.2

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

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

See also
seek()

Definition at line 1126 of file qtextstream.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read()

QString QTextStream::read ( qint64  maxlen)
Since
4.1

Reads at most maxlen characters from the stream, and returns the data read as a QString.

See also
readAll(), readLine(), QIODevice::read()

Definition at line 1624 of file qtextstream.cpp.

Here is the call graph for this function:

◆ readAll()

QString QTextStream::readAll ( )

Reads the entire content of the stream, and returns it as a QString. Avoid this function when working on large files, as it will consume a significant amount of memory.

Calling \l {QTextStream::readLine()}{readLine()} is better if you do not know how much data is available.

See also
readLine()

Definition at line 1533 of file qtextstream.cpp.

Here is the caller graph for this function:

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

If maxlen is 0, the lines can be of any length.

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, \l {QTextStream::readLine()}{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().

See also
readAll(), QIODevice::readLine()

Definition at line 1558 of file qtextstream.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ readLineInto()

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

Reads one line of text from the stream into line. If 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.

If maxlen is 0, the lines can be of any length.

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

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

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

See also
readAll(), QIODevice::readLine()

Definition at line 1591 of file qtextstream.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ realNumberNotation()

QTextStream::RealNumberNotation QTextStream::realNumberNotation ( ) const

Returns the current real number notation.

See also
setRealNumberNotation(), realNumberPrecision(), numberFlags(), integerBase()

Definition at line 1427 of file qtextstream.cpp.

Here is the caller graph for this function:

◆ realNumberPrecision()

int QTextStream::realNumberPrecision ( ) const

Returns the current real number precision, or the number of fraction digits QTextStream will write when generating real numbers.

See also
setRealNumberNotation(), realNumberNotation(), numberFlags(), integerBase()

Definition at line 1459 of file qtextstream.cpp.

◆ reset()

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.

Definition at line 1065 of file qtextstream.cpp.

Here is the caller graph for this function:

◆ resetStatus()

void QTextStream::resetStatus ( )
Since
4.1

Resets the status of the text stream.

See also
QTextStream::Status, status(), setStatus()

Definition at line 1484 of file qtextstream.cpp.

◆ seek()

bool QTextStream::seek ( qint64  pos)

Seeks to the position pos in the device. Returns true on success; otherwise returns false.

Definition at line 1087 of file qtextstream.cpp.

Here is the call graph for this function:

◆ setAutoDetectUnicode()

void QTextStream::setAutoDetectUnicode ( bool  enabled)

If enabled is 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.

See also
autoDetectUnicode(), setEncoding()

Definition at line 3000 of file qtextstream.cpp.

◆ setDevice()

void QTextStream::setDevice ( QIODevice device)

Sets the current device to device. If a device has already been assigned, QTextStream will call flush() before the old device is replaced.

Note
This function resets locale to the default locale ('C') and encoding to the default encoding, UTF-8.
See also
device(), setString()

Definition at line 1198 of file qtextstream.cpp.

Here is the call graph for this function:

◆ setEncoding()

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.

If QTextStream operates on a string, this function does nothing.

Warning
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.
See also
encoding(), setAutoDetectUnicode(), setLocale()

Definition at line 2955 of file qtextstream.cpp.

◆ setFieldAlignment()

void QTextStream::setFieldAlignment ( 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.

See also
fieldAlignment(), setFieldWidth()

Definition at line 1277 of file qtextstream.cpp.

Here is the caller graph for this function:

◆ setFieldWidth()

void QTextStream::setFieldWidth ( int  width)

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

Note
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.
See also
fieldWidth(), setPadChar()

Definition at line 1338 of file qtextstream.cpp.

Here is the caller graph for this function:

◆ setGenerateByteOrderMark()

void QTextStream::setGenerateByteOrderMark ( bool  generate)

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

See also
generateByteOrderMark(), {Qt::}{bom()}

Definition at line 3026 of file qtextstream.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setIntegerBase()

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.

See also
integerBase(), QString::number(), setNumberFlags()

Definition at line 1390 of file qtextstream.cpp.

Here is the caller graph for this function:

◆ setLocale()

void QTextStream::setLocale ( const QLocale locale)
Since
4.5

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.

See also
locale()

Definition at line 3060 of file qtextstream.cpp.

Here is the call graph for this function:

◆ setNumberFlags()

void QTextStream::setNumberFlags ( 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).

See also
numberFlags(), setIntegerBase(), setRealNumberNotation()

Definition at line 1363 of file qtextstream.cpp.

Here is the caller graph for this function:

◆ setPadChar()

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.

Example:

The string s contains:

See also
padChar(), setFieldWidth()

Definition at line 1309 of file qtextstream.cpp.

Here is the caller graph for this function:

◆ setRealNumberNotation()

void QTextStream::setRealNumberNotation ( 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.

See also
realNumberNotation(), setRealNumberPrecision(), setNumberFlags(), setIntegerBase()

Definition at line 1416 of file qtextstream.cpp.

Here is the caller graph for this function:

◆ setRealNumberPrecision()

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.

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

See also
realNumberPrecision(), setRealNumberNotation()

Definition at line 1442 of file qtextstream.cpp.

Here is the caller graph for this function:

◆ setStatus()

void QTextStream::setStatus ( Status  status)
Since
4.1

Sets the status of the text stream to the status given.

Subsequent calls to setStatus() are ignored until resetStatus() is called.

See also
Status, status(), resetStatus()

Definition at line 1500 of file qtextstream.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setString()

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

Sets the current string to string, using the given openMode. If a device has already been assigned, QTextStream will call flush() before replacing it.

See also
string(), setDevice()

Definition at line 1238 of file qtextstream.cpp.

Here is the call graph for this function:

◆ skipWhiteSpace()

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() returns true.

See also
operator>>()

Definition at line 1180 of file qtextstream.cpp.

Here is the caller graph for this function:

◆ status()

QTextStream::Status QTextStream::status ( ) const

Returns the status of the text stream.

See also
QTextStream::Status, setStatus(), resetStatus()

Definition at line 1471 of file qtextstream.cpp.

Here is the caller graph for this function:

◆ string()

QString * QTextStream::string ( ) const

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

See also
setString(), device()

Definition at line 1263 of file qtextstream.cpp.

Here is the caller graph for this function:

Friends And Related Function Documentation

◆ QDebug

friend class QDebug
friend

Definition at line 193 of file qtextstream.h.

◆ QDebugStateSaverPrivate

friend class QDebugStateSaverPrivate
friend

Definition at line 192 of file qtextstream.h.

◆ qSetFieldWidth()

QTextStreamManipulator qSetFieldWidth ( int  width)
related

Equivalent to QTextStream::setFieldWidth(width).

Definition at line 267 of file qtextstream.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ qSetPadChar()

QTextStreamManipulator qSetPadChar ( QChar  ch)
related

Equivalent to QTextStream::setPadChar(ch).

Definition at line 273 of file qtextstream.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ qSetRealNumberPrecision()

QTextStreamManipulator qSetRealNumberPrecision ( int  precision)
related

Equivalent to QTextStream::setRealNumberPrecision(precision).

Definition at line 279 of file qtextstream.h.

Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following files: