Logo Search packages:      
Sourcecode: qt4-x11 version File versions  Download package

QTextStream Class Reference

List of all members.

Detailed Description

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

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:

doc/src/snippets/code/src_corelib_io_qtextstream.cpp 0

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 codec. Example:

doc/src/snippets/code/src_corelib_io_qtextstream.cpp 1

Note that you cannot use QTextStream::atEnd(), which returns true when you have reached the end of the data stream, with stdin.

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 QTextCodec is used by QTextStream to automatically support different character sets. By default, QTextCodec::codecForLocale() is used for reading and writing, but 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 BOM (Byte Order Mark) and switch to the appropriate UTF-16 codec 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 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(). Example:

doc/src/snippets/code/src_corelib_io_qtextstream.cpp 2

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

QTextStream manipulators

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

Manipulator Description bin Same as setIntegerBase(2). oct Same as setIntegerBase(8). dec Same as setIntegerBase(10). hex Same as setIntegerBase(16). showbase Same as setNumberFlags(numberFlags() | ShowBase). forcesign Same as setNumberFlags(numberFlags() | ForceSign). forcepoint Same as setNumberFlags(numberFlags() | ForcePoint). noshowbase Same as setNumberFlags(numberFlags() & ~ShowBase). noforcesign Same as setNumberFlags(numberFlags() & ~ForceSign). noforcepoint Same as setNumberFlags(numberFlags() & ~ForcePoint). uppercasebase Same as setNumberFlags(numberFlags() | UppercaseBase). uppercasedigits Same as setNumberFlags(numberFlags() | UppercaseDigits). lowercasebase Same as setNumberFlags(numberFlags() & ~UppercaseBase). lowercasedigits Same as setNumberFlags(numberFlags() & ~UppercaseDigits). fixed Same as setRealNumberNotation(FixedNotation). scientific Same as setRealNumberNotation(ScientificNotation). left Same as setFieldAlignment(AlignLeft). right Same as setFieldAlignment(AlignRight). center Same as setFieldAlignment(AlignCenter). endl Same as operator<<('
') and flush(). flush Same as flush(). reset Same as reset(). ws Same as skipWhiteSpace(). bom Same as setGenerateByteOrderMark(true).

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

See also:
QDataStream, QIODevice, QFile, QBuffer, QTcpSocket, {Codecs Example}

Definition at line 67 of file qtextstream.h.

Public Types

enum  FieldAlignment { AlignLeft, AlignRight, AlignCenter, AlignAccountingStyle }
enum  NumberFlag {
  ShowBase = 0x1, ForcePoint = 0x2, ForceSign = 0x4, UppercaseBase = 0x8,
  UppercaseDigits = 0x10
enum  RealNumberNotation { SmartNotation, FixedNotation, ScientificNotation }
enum  Status { Ok, ReadPastEnd, ReadCorruptData }

Public Member Functions

bool atEnd () const
bool autoDetectUnicode () const
QTextCodeccodec () const
QIODevicedevice () const
FieldAlignment fieldAlignment () const
int fieldWidth () const
void flush ()
bool generateByteOrderMark () const
int integerBase () const
NumberFlags numberFlags () const
QTextStreamoperator<< (const void *ptr)
QTextStreamoperator<< (const char *c)
QTextStreamoperator<< (const QByteArray &array)
QTextStreamoperator<< (const QString &s)
QTextStreamoperator<< (double f)
QTextStreamoperator<< (float f)
QTextStreamoperator<< (qulonglong i)
QTextStreamoperator<< (qlonglong i)
QTextStreamoperator<< (unsigned long i)
QTextStreamoperator<< (signed long i)
QTextStreamoperator<< (unsigned int i)
QTextStreamoperator<< (signed int i)
QTextStreamoperator<< (unsigned short i)
QTextStreamoperator<< (signed short i)
QTextStreamoperator<< (char ch)
QTextStreamoperator<< (QChar ch)
QTextStreamoperator<< (QBool b)
QTextStreamoperator>> (char *c)
QTextStreamoperator>> (QByteArray &array)
QTextStreamoperator>> (QString &s)
QTextStreamoperator>> (double &f)
QTextStreamoperator>> (float &f)
QTextStreamoperator>> (qulonglong &i)
QTextStreamoperator>> (qlonglong &i)
QTextStreamoperator>> (unsigned long &i)
QTextStreamoperator>> (signed long &i)
QTextStreamoperator>> (unsigned int &i)
QTextStreamoperator>> (signed int &i)
QTextStreamoperator>> (unsigned short &i)
QTextStreamoperator>> (signed short &i)
QTextStreamoperator>> (char &ch)
QTextStreamoperator>> (QChar &ch)
QChar padChar () const
qint64 pos () const
 QTextStream (const QByteArray &array, QIODevice::OpenMode openMode=QIODevice::ReadOnly)
 QTextStream (QByteArray *array, QIODevice::OpenMode openMode=QIODevice::ReadWrite)
 QTextStream (QString *string, QIODevice::OpenMode openMode=QIODevice::ReadWrite)
 QTextStream (FILE *fileHandle, QIODevice::OpenMode openMode=QIODevice::ReadWrite)
 QTextStream (QIODevice *device)
 QTextStream ()
QString read (qint64 maxlen)
QString readAll ()
QString readLine (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 setNumberFlags (NumberFlags flags)
void setPadChar (QChar ch)
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
virtual ~QTextStream ()

Private Attributes

QTextStreamPrivate * d_ptr

Related Functions

(Note that these are not member functions.)

QTextStreambin (QTextStream &stream)
QTextStreambom (QTextStream &stream)
QTextStreamcenter (QTextStream &stream)
QTextStreamdec (QTextStream &stream)
QTextStreamendl (QTextStream &stream)
QTextStreamfixed (QTextStream &stream)
QTextStreamflush (QTextStream &stream)
QTextStreamforcepoint (QTextStream &stream)
QTextStreamforcesign (QTextStream &stream)
QTextStreamhex (QTextStream &stream)
QTextStreamleft (QTextStream &stream)
QTextStreamlowercasebase (QTextStream &stream)
QTextStreamlowercasedigits (QTextStream &stream)
QTextStreamnoforcepoint (QTextStream &stream)
QTextStreamnoforcesign (QTextStream &stream)
QTextStreamnoshowbase (QTextStream &stream)
QTextStreamoct (QTextStream &stream)
QTextStreamManipulator qSetFieldWidth (int width)
QTextStreamManipulator qSetPadChar (QChar ch)
QTextStreamManipulator qSetRealNumberPrecision (int precision)
QTextStreamreset (QTextStream &stream)
QTextStreamright (QTextStream &stream)
QTextStreamscientific (QTextStream &stream)
QTextStreamshowbase (QTextStream &stream)
QTextStreamuppercasebase (QTextStream &stream)
QTextStreamuppercasedigits (QTextStream &stream)
QTextStreamws (QTextStream &stream)

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

Generated by  Doxygen 1.6.0   Back to index