CopperSpice API  1.9.1
QTextStream Class Reference

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

Public Typedefs

using NumberFlags = QFlags< NumberFlag >
 

Public Types

enum  FieldAlignment
 
enum  NumberFlag
 
enum  RealNumberNotation
 
enum  Status
 

Public Methods

 QTextStream ()
 
 QTextStream (const QByteArray &array, QIODevice::OpenMode openMode=QIODevice::ReadOnly)
 
 QTextStream (FILE *fileHandle, QIODevice::OpenMode openMode=QIODevice::ReadWrite)
 
 QTextStream (QByteArray *array, QIODevice::OpenMode openMode=QIODevice::ReadWrite)
 
 QTextStream (QIODevice *device)
 
 QTextStream (QString *string, QIODevice::OpenMode openMode=QIODevice::ReadWrite)
 
virtual ~QTextStream ()
 
bool atEnd () const
 
bool autoDetectUnicode () const
 
QTextCodeccodec () const
 
QIODevicedevice () const
 
FieldAlignment fieldAlignment () const
 
int fieldWidth () const
 
void flush ()
 
bool generateByteOrderMark () const
 
Params getParams () const
 
int integerBase () const
 
QLocale locale () const
 
NumberFlags numberFlags () const
 
QTextStream & operator<< (bool b)
 
QTextStream & operator<< (char ch)
 
QTextStream & operator<< (const char *str)
 
QTextStream & operator<< (const QByteArray &array)
 
QTextStream & operator<< (const QString &str)
 
QTextStream & operator<< (const QStringView &str)
 
QTextStream & operator<< (const void *ptr)
 
QTextStream & operator<< (double f)
 
QTextStream & operator<< (float f)
 
QTextStream & operator<< (QChar ch)
 
QTextStream & operator<< (qint64 i)
 
QTextStream & operator<< (quint64 i)
 
QTextStream & operator<< (signed int i)
 
QTextStream & operator<< (signed long i)
 
QTextStream & operator<< (signed short i)
 
QTextStream & operator<< (unsigned int i)
 
QTextStream & operator<< (unsigned long i)
 
QTextStream & operator<< (unsigned short i)
 
QTextStream & operator>> (char &ch)
 
QTextStream & operator>> (double &f)
 
QTextStream & operator>> (float &f)
 
QTextStream & operator>> (QByteArray &array)
 
QTextStream & operator>> (QChar &ch)
 
QTextStream & operator>> (qint64 &i)
 
QTextStream & operator>> (QString &str)
 
QTextStream & operator>> (quint64 &i)
 
QTextStream & operator>> (signed int &i)
 
QTextStream & operator>> (signed long &i)
 
QTextStream & operator>> (signed short &i)
 
QTextStream & operator>> (unsigned int &i)
 
QTextStream & operator>> (unsigned long &i)
 
QTextStream & operator>> (unsigned short &i)
 
QChar padChar () const
 
qint64 pos () const
 
QString read (qint64 maxlen)
 
QString readAll ()
 
QString readLine (qint64 maxlen=0)
 
bool readLineInto (QString *line, qint64 maxlen=0)
 
RealNumberNotation realNumberNotation () const
 
int realNumberPrecision () const
 
void reset ()
 
void resetStatus ()
 
bool seek (qint64 pos)
 
void setAutoDetectUnicode (bool enabled)
 
void setCodec (const char *codecName)
 
void setCodec (QTextCodec *codec)
 
void setDevice (QIODevice *device)
 
void setFieldAlignment (FieldAlignment alignment)
 
void setFieldWidth (int width)
 
void setGenerateByteOrderMark (bool generate)
 
void setIntegerBase (int base)
 
void setLocale (const QLocale &locale)
 
void setNumberFlags (NumberFlags flags)
 
void setPadChar (QChar ch)
 
void setParams (const Params &data)
 
void setRealNumberNotation (RealNumberNotation notation)
 
void setRealNumberPrecision (int precision)
 
void setStatus (Status status)
 
void setString (QString *string, QIODevice::OpenMode openMode=QIODevice::ReadWrite)
 
void skipWhiteSpace ()
 
Status status () const
 
QStringstring () const
 

Friends

class QDebug
 

Related Functions

These are not member functions

QTextStream & bin (QTextStream &stream)
 
QTextStream & bom (QTextStream &stream)
 
QTextStream & center (QTextStream &stream)
 
QTextStream & dec (QTextStream &stream)
 
QTextStream & endl (QTextStream &stream)
 
QTextStream & fixed (QTextStream &stream)
 
QTextStream & flush (QTextStream &stream)
 
QTextStream & forcepoint (QTextStream &stream)
 
QTextStream & forcesign (QTextStream &stream)
 
QTextStream & hex (QTextStream &stream)
 
QTextStream & left (QTextStream &stream)
 
QTextStream & lowercasebase (QTextStream &stream)
 
QTextStream & lowercasedigits (QTextStream &stream)
 
QTextStream & noforcepoint (QTextStream &stream)
 
QTextStream & noforcesign (QTextStream &stream)
 
QTextStream & noshowbase (QTextStream &stream)
 
QTextStream & oct (QTextStream &stream)
 
QTextStreamManipulator qSetFieldWidth (int width)
 
QTextStreamManipulator qSetPadChar (QChar ch)
 
QTextStreamManipulator qSetRealNumberPrecision (int precision)
 
QTextStream & reset (QTextStream &stream)
 
QTextStream & right (QTextStream &stream)
 
QTextStream & scientific (QTextStream &stream)
 
QTextStream & showbase (QTextStream &stream)
 
QTextStream & uppercasebase (QTextStream &stream)
 
QTextStream & uppercasedigits (QTextStream &stream)
 
QTextStream & ws (QTextStream &stream)
 

Detailed Description

The QTextStream class provides a interface for reading and writing text. This class 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.

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 is 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 codec.

QTextStream stream(stdin);
QString line;
do {
line = stream.readLine();
} while (! line.isNull());

Besides using the QTextStream 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. The QTextCodec class is used by QTextStream to automatically support different character sets. By default QTextCodec::codecForLocale() is used for reading and writing. You can also set the codec by calling setCodec(). Automatic Unicode detection is also supported. When this feature is enabled (the default behavior), QTextStream will detect the UTF-16 or the UTF-32 BOM (Byte Order Mark) and switch to the appropriate UTF codec when reading. QTextStream does not write a BOM by default, this can be enabled by calling setGenerateByteOrderMark(true). When QTextStream operates on a QString directly the codec is disabled.

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

  • Chunk by chunk, by calling readLine() or readAll().
  • Word by word. QTextStream supports streaming into QStrings, QByteArrays 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() 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().

Like <iostream> in the standard C++ library, QTextStream also defines several global manipulator functions as shown below. In addition to these functions there are three global manipulators which take a parameter: qSetFieldWidth(), qSetPadChar(), and qSetRealNumberPrecision().

ManipulatorDescription
binSame as setIntegerBase(2)
octSame as setIntegerBase(8)
decSame as setIntegerBase(10)
hexSame as setIntegerBase(16)
showbaseSame as setNumberFlags(numberFlags() | ShowBase)
forcesignSame as setNumberFlags(numberFlags() | ForceSign)
forcepointSame as setNumberFlags(numberFlags() | ForcePoint)
noshowbaseSame as setNumberFlags(numberFlags() & ~ShowBase)
noforcesignSame as setNumberFlags(numberFlags() & ~ForceSign)
noforcepointSame as setNumberFlags(numberFlags() & ~ForcePoint)
uppercasebaseSame as setNumberFlags(numberFlags() | UppercaseBase)
uppercasedigitsSame as setNumberFlags(numberFlags() | UppercaseDigits)
lowercasebaseSame as setNumberFlags(numberFlags() & ~UppercaseBase)
lowercasedigitsSame as setNumberFlags(numberFlags() & ~UppercaseDigits)
fixedSame as setRealNumberNotation(FixedNotation)
scientificSame as setRealNumberNotation(ScientificNotation)
leftSame as setFieldAlignment(AlignLeft)
rightSame as setFieldAlignment(AlignRight)
centerSame as setFieldAlignment(AlignCenter)
endlSame as operator<<('\n') and flush()
flushSame as flush()
resetSame as reset()
wsSame as skipWhiteSpace()
bomSame as setGenerateByteOrderMark(true)
See also
QDataStream, QIODevice, QFile, QBuffer, QTcpSocket

Member Typedef Documentation

Typedef for QFlags<NumberFlag>. Refer to QTextStream::NumberFlag for documentation.

Member Enumeration Documentation

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

ConstantValueDescription
QTextStream::AlignLeft0Pad on the right side of fields.
QTextStream::AlignRight1Pad on the left side of fields.
QTextStream::AlignCenter2Pad on both sides of field.
QTextStream::AlignAccountingStyle3Same as AlignRight, except that the sign of a number is flush left.
See also
setFieldAlignment()

This enum specifies various flags that can be set to affect the output of integers, floats, and doubles.

ConstantValueDescription
QTextStream::ShowBase0x1Show the base as a prefix if the base is 16 ("0x"), 8 ("0"), or 2 ("0b").
QTextStream::ForcePoint0x2Always put the decimal separator in numbers, even if there are no decimals.
QTextStream::ForceSign0x4Always put the sign in numbers, even for positive numbers.
QTextStream::UppercaseBase0x8Use uppercase versions of base prefixes ("0X", "0B").
QTextStream::UppercaseDigits0x10Use uppercase letters for expressing digits 10 to 35 instead of lowercase.
See also
setNumberFlags()

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

ConstantValueDescription
QTextStream::SmartNotation0Scientific or fixed-point notation, depending on which makes most sense (printf()'s g flag).
QTextStream::FixedNotation1Fixed-point notation (printf()'s f flag).
QTextStream::ScientificNotation2Scientific notation (printf()'s e flag).
See also
setRealNumberNotation()

This enum describes the current status of the text stream.

ConstantValueDescription
QTextStream::Ok0The text stream is operating normally.
QTextStream::ReadPastEnd1The text stream has read past the end of the data in the underlying device.
QTextStream::ReadCorruptData2The text stream has read corrupt data.
QTextStream::WriteFailed3The text stream can not write to the underlying device.
See also
status()

Constructor & Destructor Documentation

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()
QTextStream::QTextStream ( QIODevice device)
explicit

Constructs a QTextStream that operates on device.

QTextStream::QTextStream ( FILE *  fileHandle,
QIODevice::OpenMode  openMode = QIODevice::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.

QString str;
QTextStream in(stdin);
in >> str;
QTextStream::QTextStream ( QString string,
QIODevice::OpenMode  openMode = QIODevice::ReadWrite 
)
explicit

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

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

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

QTextStream::QTextStream ( const QByteArray array,
QIODevice::OpenMode  openMode = QIODevice::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.

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;
...
}
}
QTextStream::~QTextStream ( )
virtual

Destroys the QTextStream.

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

Method Documentation

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.

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(), setCodec()
QTextCodec * QTextStream::codec ( ) const

Returns the codec that is current assigned to the stream.

See also
setCodec(), setAutoDetectUnicode(), locale()
QIODevice * QTextStream::device ( ) const

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

See also
setDevice(), string()
FieldAlignment QTextStream::fieldAlignment ( ) const

Returns the current field alignment.

See also
setFieldAlignment(), fieldWidth()
int QTextStream::fieldWidth ( ) const

Returns the current field width.

See also
setFieldWidth()
void QTextStream::flush ( )

Flushes any buffered data waiting to be written to the device. If QTextStream operates on a string, this method does nothing.

bool QTextStream::generateByteOrderMark ( ) const

Returns true if QTextStream is set to generate the UTF BOM (Byte Order Mark) when using a UTF codec, otherwise returns false. UTF BOM generation is set to false by default.

See also
setGenerateByteOrderMark()
Params QTextStream::getParams ( ) const

Retrieve the current state of the QTextStream.

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(), QString8::number(), numberFlags()
QLocale QTextStream::locale ( ) const

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

See also
setLocale()
NumberFlags QTextStream::numberFlags ( ) const

Returns the current number flags.

See also
setNumberFlags(), integerBase(), realNumberNotation()
QTextStream & QTextStream::operator<< ( bool  b)

Writes b to the stream and returns a reference to the QTextStream.

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

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

QTextStream & QTextStream::operator<< ( const char *  str)
inline

Writes the constant string pointed to by str to the stream. The value for str is assumed to be in Latin1 encoding. This operator is convenient when working with constant string data.

QTextStream out(stdout);
out << "CopperSpice is awesome" << Qt::endl;
QTextStream & QTextStream::operator<< ( const QByteArray array)

Writes array to the stream.

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

Writes the string str to the stream, and returns a reference to the QTextStream. The string is first encoded using the assigned codec (the default codec is QTextCodec::codecForLocale()) before it is written to the stream.

See also
setFieldWidth(), setCodec()
QTextStream & QTextStream::operator<< ( const QStringView str)

Writes str to the stream and returns a reference to the QTextStream.

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

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

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

Writes the double f to the stream.

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()
QTextStream & QTextStream::operator<< ( QChar  ch)

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

See also
setFieldWidth()
QTextStream & QTextStream::operator<< ( qint64  i)

Writes the qint64 i to the stream.

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

Writes the quint64 i to the stream.

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

Writes the signed int i to the stream.

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

Writes the signed long i to the stream.

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. The base can be also be set by calling setIntegerBase().

See also
setFieldWidth(), setNumberFlags()
QTextStream & QTextStream::operator<< ( unsigned int  i)

Writes the unsigned int i to the stream.

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

Writes the unsigned long i to the stream.

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

Writes the unsigned short i to the stream.

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

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

See also
QChar32::toLatin1()
QTextStream & QTextStream::operator>> ( double &  f)

Stores the real number in the double f.

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>> ( QByteArray array)

Converts the word to ISO-8859-1, then stores it in array.

See also
QString8::toLatin1()
QTextStream & QTextStream::operator>> ( QChar ch)

Reads a character from the stream and stores it in ch. Returns a reference to the QTextStream, so several operators can be nested.

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

Whitespace is not skipped.

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

Stores the integer in the qint64 i.

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.

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

Stores the integer in the quint64 i.

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

Stores the integer in the signed int i.

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

Stores the integer in the signed long i.

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, then i is set to 0.

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

PrefixBase
"0b" or "0B"2 (binary)
"0" followed by "0-7"8 (octal)
"0" otherwise10 (decimal)
"0x" or "0X"16 (hexadecimal)
"1" to "9"10 (decimal)

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

QChar QTextStream::padChar ( ) const

Returns the current pad character.

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

Because QTextStream is buffered, this method 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 method in a tight loop.

See also
seek()
QString QTextStream::read ( qint64  maxlen)

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

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

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

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

See also
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 which are longer than maxlen, the lines will be split after maxlen number of characters and returned in segments. If maxlen is 0 the lines can be of any length.

This method discards any trailing end-of-line characters such as "\n" or "\r\n".

If the stream has read to the end of the file readLine() will return an empty QString.

See also
readAll(), QIODevice::readLine()
bool QTextStream::readLineInto ( QString line,
qint64  maxlen = 0 
)

Reads one line of text from the stream into line. If line is 0, 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 method 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()

Reads one line of text from the stream and saves it to the buffer line points to. If line is a nullptr then no data read from the stream will be saved.

The maximum allowed line length is set to maxlen. If the stream contains lines which are longer than maxlen, the lines will be split after maxlen number of characters and returned in segments. If maxlen is 0 the lines can be of any length.

This method discards any trailing end-of-line characters such as "\n" or "\r\n". If the buffer for line is large enough to store the entire line length read from the stream, then this method is faster than calling readLine().

Returns false if the stream has read to the end of the file or an error has occurred, otherwise returns true. The string pointed to line are discarded ?.

See also
readAll(), QIODevice::readLine()
RealNumberNotation QTextStream::realNumberNotation ( ) const

Returns the current real number notation.

See also
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.

See also
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.

See also
QTextStream::Status, status(), setStatus()
bool QTextStream::seek ( qint64  pos)

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

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-16 or UTF-32 BOM (Byte Order Mark). If this mark is found, QTextStream will replace the current codec with the UTF codec.

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

See also
autoDetectUnicode(), setCodec()
void QTextStream::setCodec ( const char *  codecName)

Sets the codec for this stream to the QTextCodec for the encoding specified by codecName. Common values for codecName include "ISO 8859-1", "UTF-8", and "UTF-16". If the encoding is not recognized, nothing happens.

QTextStream out(&file);
out.setCodec("UTF-8");
See also
QTextCodec::codecForName(), setLocale()
void QTextStream::setCodec ( QTextCodec codec)

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

If QTextStream operates on a string this method does nothing.

Warning
If you call this method while the text stream is reading from an open sequential socket, the internal buffer may still contain text decoded using the old codec.
See also
codec(), setAutoDetectUnicode(), setLocale()
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 method resets locale to the default locale ('C') and codec to the default codec, QTextCodec::codecForLocale().
See also
device(), setString()
void QTextStream::setFieldAlignment ( FieldAlignment  alignment)

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

See also
fieldAlignment(), 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()
void QTextStream::setGenerateByteOrderMark ( bool  generate)

If generate is true and a UTF codec 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(), 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.

See also
integerBase(), QString8::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.

See also
locale()
void QTextStream::setNumberFlags ( NumberFlags  flags)

Sets the current number flags to flags. The value for 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()
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.

QTextStream out(&s);
out.setFieldWidth(20);
out.setFieldAlignment(QTextStream::AlignCenter);
out.setPadChar('-');
out << "CopperSpice" << "is cool."; // displays: -----CopperSpice----------is cool.------
See also
padChar(), setFieldWidth()
void QTextStream::setParams ( const Params &  data)

Set the current state of the QTextStream to the values previously stored in data.

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()
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 can not be a negative value. The default value is 6.

See also
realNumberPrecision(), setRealNumberNotation()
void QTextStream::setStatus ( Status  status)

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()
void QTextStream::setString ( QString string,
QIODevice::OpenMode  openMode = QIODevice::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()
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>>()
Status QTextStream::status ( ) const

Returns the status of the text stream.

See also
QTextStream::Status, setStatus(), resetStatus()
QString * QTextStream::string ( ) const

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

See also
setString(), device()

Friends And Related Function Documentation

QTextStream & bin ( QTextStream &  stream)
related

Calls QTextStream::setIntegerBase(2) on stream and returns a reference to stream.

See also
oct(), dec(), hex(), QTextStream manipulators
QTextStream & bom ( QTextStream &  stream)
related

Toggles insertion of the Byte Order Mark on stream when QTextStream is used with a UTF codec.

See also
QTextStream::setGenerateByteOrderMark(), QTextStream manipulators
QTextStream & center ( QTextStream &  stream)
related

Calls QTextStream::setFieldAlignment(QTextStream::AlignCenter) on stream and returns a reference to stream.

See also
left(), right(), QTextStream manipulators
QTextStream & dec ( QTextStream &  stream)
related

Calls QTextStream::setIntegerBase(10) on stream and returns stream.

See also
bin(), oct(), hex(), QTextStream manipulators
QTextStream & endl ( QTextStream &  stream)
related

Writes '\n' to the stream and flushes the stream.

This is equivalent to the following:

stream << '\n' << flush;

On Windows all '\n' characters are written as '\r\n' if the QTextStream device or string is opened using the QIODevice::Text flag.

See also
flush(), reset(), QTextStream manipulators
QTextStream & fixed ( QTextStream &  stream)
related

Uses stream to set the number notation flag to QTextStream::FixedNotation. Equivalent to the following code.

stream.setRealNumberNotation(QTextStream::FixedNotation);
See also
scientific(), QTextStream manipulators
QTextStream & flush ( QTextStream &  stream)
related

Flushes the given stream.

See also
endl(), reset(), QTextStream manipulators
QTextStream & forcepoint ( QTextStream &  stream)
related

Uses stream to set the floating point flag to QTextStream::ForcePoint. Equivalent to the following code.

stream.setNumberFlags(stream.numberFlags() | QTextStream::ForcePoint);
See also
noforcepoint(), forcesign(), showbase(), QTextStream manipulators
QTextStream & forcesign ( QTextStream &  stream)
related

Uses stream to set the sign flag to QTextStream::ForceSign. Equivalent to the following code.

stream.setNumberFlags(stream.numberFlags() | QTextStream::ForceSign);
See also
noforcesign(), forcepoint(), showbase(), QTextStream manipulators
QTextStream & hex ( QTextStream &  stream)
related

Uses stream to set the integer base to 16. Equivalent to the following code.

stream.setIntegerBase(16);
See also
bin(), oct>(), dec(), QTextStream manipulators
QTextStream & left ( QTextStream &  stream)
related

Uses stream to set the alignment to QTextStream::AlignLeft. Equivalent to the following code.

stream.setFieldAlignment(QTextStream::AlignLeft);
See also
right(), center(), QTextStream manipulators
QTextStream & lowercasebase ( QTextStream &  stream)
related

Uses stream to turn off the flag QTextStream::UppercaseBase. Equivalent to the following code.

stream.setNumberFlags(stream.numberFlags() & ~QTextStream::UppercaseBase);
See also
uppercasebase(), lowercasedigits(), QTextStream manipulators
QTextStream & lowercasedigits ( QTextStream &  stream)
related

Uses stream to turn off the flag QTextStream::UppercaseDigits. Equivalent to the following code.

stream.setNumberFlags(stream.numberFlags() & ~QTextStream::UppercaseDigits);
See also
uppercasedigits(), lowercasebase(), QTextStream manipulators
QTextStream & noforcepoint ( QTextStream &  stream)
related

Uses stream to turn off the flag QTextStream::ForcePoint. Equivalent to the following code.

stream.setNumberFlags(stream.numberFlags() & ~QTextStream::ForcePoint);
See also
forcepoint(), noforcesign(), noshowbase(), QTextStream manipulators
QTextStream & noforcesign ( QTextStream &  stream)
related

Uses stream to turn off the flag QTextStream::ForceSign. Equivalent to the following code.

stream.setNumberFlags(stream.numberFlags() & ~QTextStream::ForceSign);
See also
forcesign(), noforcepoint(), noshowbase(), QTextStream manipulators
QTextStream & noshowbase ( QTextStream &  stream)
related

Uses stream to turn off the flag QTextStream::ShowBase. Equivalent to the following code.

stream.setNumberFlags(stream.numberFlags() & ~QTextStream::ShowBase);
See also
showbase(), noforcesign(), noforcepoint(), QTextStream manipulators
QTextStream & oct ( QTextStream &  stream)
related

Calls QTextStream::setIntegerBase(8) on stream and returns a reference to stream.

See also
bin(), dec(), hex(), QTextStream manipulators
QTextStreamManipulator qSetFieldWidth ( int  width)
related

Equivalent to QTextStream::setFieldWidth(width).

QTextStreamManipulator qSetPadChar ( QChar  ch)
related

Equivalent to QTextStream::setPadChar( ch ).

QTextStreamManipulator qSetRealNumberPrecision ( int  precision)
related

Equivalent to QTextStream::setRealNumberPrecision( precision ).

QTextStream & reset ( QTextStream &  stream)
related

Calls QTextStream::reset() on stream and returns a reference to stream.

See also
flush(), QTextStream manipulators
QTextStream & right ( QTextStream &  stream)
related

Calls QTextStream::setFieldAlignment(QTextStream::AlignRight) on stream and returns a reference to stream.

See also
left(), center(), QTextStream manipulators
QTextStream & scientific ( QTextStream &  stream)
related

Calls QTextStream::setRealNumberNotation(QTextStream::ScientificNotation) on stream and returns a reference to stream.

See also
fixed(), QTextStream manipulators
QTextStream & showbase ( QTextStream &  stream)
related

Calls QTextStream::setNumberFlags(QTextStream::numberFlags() | QTextStream::ShowBase) on stream and returns a reference to stream.

See also
noshowbase(), forcesign(), forcepoint(), QTextStream manipulators
QTextStream & uppercasebase ( QTextStream &  stream)
related

Calls QTextStream::setNumberFlags(QTextStream::numberFlags() | QTextStream::UppercaseBase) on stream and returns a reference to stream.

See also
lowercasebase(), uppercasedigits(), QTextStream manipulators
QTextStream & uppercasedigits ( QTextStream &  stream)
related

Calls QTextStream::setNumberFlags(QTextStream::numberFlags() | QTextStream::UppercaseDigits) on stream and returns a reference to stream.

See also
lowercasedigits(), uppercasebase(), QTextStream manipulators
QTextStream & ws ( QTextStream &  stream)
related

Calls skipWhiteSpace() on stream and returns a reference to stream.

See also
QTextStream manipulators