Table Of Contents

Previous topic

QTemporaryFile

Next topic

QProcess

QTextStream

Inheritance diagram of QTextStream

Synopsis

Functions

Detailed Description

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

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

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

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

stream = QTextStream(sys.stdin.fileno())

while(True):
    line = stream.readLine()
    if line.isNull():
        break;

Note that you cannot use QTextStream.atEnd() , which returns true when you have reached the end of the data stream, with stdin. The reason for this is that as long as stdin doesn’t give any input to the PySide.QtCore.QTextStream , atEnd() will return true even if the stdin is open and waiting for more characters.

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

Internally, PySide.QtCore.QTextStream uses a Unicode based buffer, and PySide.QtCore.QTextCodec is used by PySide.QtCore.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 PySide.QtCore.QTextStream.setCodec() . Automatic Unicode detection is also supported. When this feature is enabled (the default behavior), PySide.QtCore.QTextStream will detect the UTF-16 or the UTF-32 BOM (Byte Order Mark) and switch to the appropriate UTF codec when reading. PySide.QtCore.QTextStream does not write a BOM by default, but you can enable this by calling setGenerateByteOrderMark(true). When PySide.QtCore.QTextStream operates on a PySide.QtCore.QString directly, the codec is disabled.

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

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 PySide.QtCore.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, PySide.QtCore.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 PySide.QtCore.QTextStream.setIntegerBase() . Example:

in_ = QTextStream("0x50 0x20")
firstNumber = 0
secondNumber = 0

in_ >> firstNumber             # firstNumber == 80
in_ >> dec >> secondNumber     # secondNumber == 0

ch = None
in_ >> ch                      # ch == 'x'

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

Like <iostream> in the standard C++ library, PySide.QtCore.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( PySide.QtCore.QTextStream.numberFlags() | ShowBase ).
forcesign Same as setNumberFlags( PySide.QtCore.QTextStream.numberFlags() | ForceSign ).
forcepoint Same as setNumberFlags( PySide.QtCore.QTextStream.numberFlags() | ForcePoint ).
noshowbase Same as setNumberFlags( PySide.QtCore.QTextStream.numberFlags() & ~ ShowBase ).
noforcesign Same as setNumberFlags( PySide.QtCore.QTextStream.numberFlags() & ~ ForceSign ).
noforcepoint Same as setNumberFlags( PySide.QtCore.QTextStream.numberFlags() & ~ ForcePoint ).
uppercasebase Same as setNumberFlags( PySide.QtCore.QTextStream.numberFlags() | UppercaseBase ).
uppercasedigits Same as setNumberFlags( PySide.QtCore.QTextStream.numberFlags() | UppercaseDigits ).
lowercasebase Same as setNumberFlags( PySide.QtCore.QTextStream.numberFlags() & ~ UppercaseBase ).
lowercasedigits Same as setNumberFlags( PySide.QtCore.QTextStream.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<<(‘n’) and PySide.QtCore.QTextStream.flush() .
flush Same as PySide.QtCore.QTextStream.flush() .
reset Same as PySide.QtCore.QTextStream.reset() .
ws Same as PySide.QtCore.QTextStream.skipWhiteSpace() .
bom Same as setGenerateByteOrderMark(true).

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

class PySide.QtCore.QTextStream
class PySide.QtCore.QTextStream(array[, openMode=QIODevice.ReadWrite])
class PySide.QtCore.QTextStream(device)
Parameters:

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

See also

PySide.QtCore.QTextStream.setDevice() PySide.QtCore.QTextStream.setString()

Constructs a PySide.QtCore.QTextStream that operates on device .

PySide.QtCore.QTextStream.RealNumberNotation

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

Constant Description
QTextStream.ScientificNotation Scientific notation (printf()‘s %e flag).
QTextStream.FixedNotation Fixed-point notation (printf()‘s %f flag).
QTextStream.SmartNotation Scientific or fixed-point notation, depending on which makes most sense (printf()‘s %g flag).
PySide.QtCore.QTextStream.NumberFlag

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

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

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

Constant Description
QTextStream.AlignLeft Pad on the right side of fields.
QTextStream.AlignRight Pad on the left side of fields.
QTextStream.AlignCenter Pad on both sides of field.
QTextStream.AlignAccountingStyle Same as AlignRight , except that the sign of a number is flush left.
PySide.QtCore.QTextStream.Status

This enum describes the current status of the text stream.

Constant Description
QTextStream.Ok The text stream is operating normally.
QTextStream.ReadPastEnd The text stream has read past the end of the data in the underlying device.
QTextStream.ReadCorruptData The text stream has read corrupt data.
PySide.QtCore.QTextStream.atEnd()
Return type:PySide.QtCore.bool

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

PySide.QtCore.QTextStream.autoDetectUnicode()
Return type:PySide.QtCore.bool

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

PySide.QtCore.QTextStream.codec()
Return type:PySide.QtCore.QTextCodec

Returns the codec that is current assigned to the stream.

PySide.QtCore.QTextStream.device()
Return type:PySide.QtCore.QIODevice

Returns the current device associated with the PySide.QtCore.QTextStream , or 0 if no device has been assigned.

PySide.QtCore.QTextStream.fieldAlignment()
Return type:PySide.QtCore.QTextStream.FieldAlignment

Returns the current field alignment.

PySide.QtCore.QTextStream.fieldWidth()
Return type:PySide.QtCore.int

Returns the current field width.

PySide.QtCore.QTextStream.flush()

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

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

PySide.QtCore.QTextStream.generateByteOrderMark()
Return type:PySide.QtCore.bool

Returns true if PySide.QtCore.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.

PySide.QtCore.QTextStream.integerBase()
Return type:PySide.QtCore.int

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

PySide.QtCore.QTextStream.locale()
Return type:PySide.QtCore.QLocale

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

PySide.QtCore.QTextStream.numberFlags()
Return type:PySide.QtCore.QTextStream.NumberFlags

Returns the current number flags.

PySide.QtCore.QTextStream.__lshift__(i)
Parameters:iPySide.QtCore.signed long
Return type:PySide.QtCore.QTextStream

This is an overloaded function.

Writes the signed long i to the stream.

PySide.QtCore.QTextStream.__lshift__(i)
Parameters:i – long
Return type:PySide.QtCore.QTextStream

This is an overloaded function.

Writes the unsigned long i to the stream.

PySide.QtCore.QTextStream.__lshift__(f)
Parameters:fPySide.QtCore.double
Return type:PySide.QtCore.QTextStream

This is an overloaded function.

Writes the double f to the stream.

PySide.QtCore.QTextStream.__lshift__(arg__2)
Parameters:arg__2PySide.QtGui.QSplitter
Return type:PySide.QtCore.QTextStream
PySide.QtCore.QTextStream.__lshift__(ch)
Parameters:chPySide.QtCore.char
Return type:PySide.QtCore.QTextStream

This is an overloaded function.

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

PySide.QtCore.QTextStream.__lshift__(m)
Parameters:mPySide.QtCore.QTextStreamManipulator
Return type:PySide.QtCore.QTextStream
PySide.QtCore.QTextStream.__lshift__(ch)
Parameters:chPySide.QtCore.QChar
Return type:PySide.QtCore.QTextStream

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

PySide.QtCore.QTextStream.__lshift__(s)
Parameters:s – unicode
Return type:PySide.QtCore.QTextStream

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

PySide.QtCore.QTextStream.__lshift__(array)
Parameters:arrayPySide.QtCore.QByteArray
Return type:PySide.QtCore.QTextStream

This is an overloaded function.

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

PySide.QtCore.QTextStream.__lshift__(arg__2)
Parameters:arg__2PySide.QtXml.QDomNode
Return type:PySide.QtCore.QTextStream
PySide.QtCore.QTextStream.__rshift__(array)
Parameters:arrayPySide.QtCore.QByteArray
Return type:PySide.QtCore.QTextStream

This is an overloaded function.

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

See also

QString.toLatin1()

PySide.QtCore.QTextStream.__rshift__(arg__2)
Parameters:arg__2PySide.QtGui.QSplitter
Return type:PySide.QtCore.QTextStream
PySide.QtCore.QTextStream.padChar()
Return type:PySide.QtCore.QChar

Returns the current pad character.

PySide.QtCore.QTextStream.pos()
Return type:PySide.QtCore.qint64

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

PySide.QtCore.QTextStream.read(maxlen)
Parameters:maxlenPySide.QtCore.qint64
Return type:unicode

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

PySide.QtCore.QTextStream.readAll()
Return type:unicode

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

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

PySide.QtCore.QTextStream.readLine([maxlen=0])
Parameters:maxlenPySide.QtCore.qint64
Return type:unicode

Reads one line of text from the stream, and returns it as a PySide.QtCore.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. A common value for maxlen is 75.

The returned line has no trailing end-of-line characters (“n” or “rn”), so calling QString.trimmed() is unnecessary.

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

PySide.QtCore.QTextStream.realNumberNotation()
Return type:PySide.QtCore.QTextStream.RealNumberNotation

Returns the current real number notation.

PySide.QtCore.QTextStream.realNumberPrecision()
Return type:PySide.QtCore.int

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

PySide.QtCore.QTextStream.reset()

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

PySide.QtCore.QTextStream.resetStatus()

Resets the status of the text stream.

PySide.QtCore.QTextStream.seek(pos)
Parameters:posPySide.QtCore.qint64
Return type:PySide.QtCore.bool

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

PySide.QtCore.QTextStream.setAutoDetectUnicode(enabled)
Parameters:enabledPySide.QtCore.bool

If enabled is true, PySide.QtCore.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, PySide.QtCore.QTextStream will replace the current codec with the UTF codec.

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

PySide.QtCore.QTextStream.setCodec(codecName)
Parameters:codecName – str

Sets the codec for this stream to the PySide.QtCore.QTextCodec for the encoding specified by codecName . Common values for codecName include “ISO 8859-1”, “UTF-8”, and “UTF-16”. If the encoding isn’t recognized, nothing happens.

Example:

out = QTextStream(file)
out.setCodec("UTF-8")
PySide.QtCore.QTextStream.setCodec(codec)
Parameters:codecPySide.QtCore.QTextCodec

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 PySide.QtCore.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 codec.

PySide.QtCore.QTextStream.setDevice(device)
Parameters:devicePySide.QtCore.QIODevice

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

Note

This function resets locale to the default locale (‘C’) and codec to the default codec, QTextCodec.codecForLocale() .

See also

PySide.QtCore.QTextStream.device() PySide.QtCore.QTextStream.setString()

PySide.QtCore.QTextStream.setFieldAlignment(alignment)
Parameters:alignmentPySide.QtCore.QTextStream.FieldAlignment

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

PySide.QtCore.QTextStream.setFieldWidth(width)
Parameters:widthPySide.QtCore.int

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.

PySide.QtCore.QTextStream.setGenerateByteOrderMark(generate)
Parameters:generatePySide.QtCore.bool

If generate is true and a UTF codec is used, PySide.QtCore.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.

PySide.QtCore.QTextStream.setIntegerBase(base)
Parameters:basePySide.QtCore.int

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, PySide.QtCore.QTextStream will attempt to detect the base by inspecting the data on the stream. When generating numbers, PySide.QtCore.QTextStream assumes base is 10 unless the base has been set explicitly.

PySide.QtCore.QTextStream.setLocale(locale)
Parameters:localePySide.QtCore.QLocale

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.

PySide.QtCore.QTextStream.setNumberFlags(flags)
Parameters:flagsPySide.QtCore.QTextStream.NumberFlags
PySide.QtCore.QTextStream.setPadChar(ch)
Parameters:chPySide.QtCore.QChar

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

Example:

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

The string s contains:

----Qt------rocks!--
PySide.QtCore.QTextStream.setRealNumberNotation(notation)
Parameters:notationPySide.QtCore.QTextStream.RealNumberNotation

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

PySide.QtCore.QTextStream.setRealNumberPrecision(precision)
Parameters:precisionPySide.QtCore.int

Sets the precision of real numbers to precision . This value describes the number of fraction digits PySide.QtCore.QTextStream should write when generating real numbers.

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

PySide.QtCore.QTextStream.setStatus(status)
Parameters:statusPySide.QtCore.QTextStream.Status

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

PySide.QtCore.QTextStream.skipWhiteSpace()

Reads and discards whitespace from the stream until either a non-space character is detected, or until PySide.QtCore.QTextStream.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

PySide.QtCore.QTextStream.operator>>()

PySide.QtCore.QTextStream.status()
Return type:PySide.QtCore.QTextStream.Status

Returns the status of the text stream.

PySide.QtCore.QTextStream.string()
Return type:unicode

Returns the current string assigned to the PySide.QtCore.QTextStream , or 0 if no string has been assigned.

See also

PySide.QtCore.QTextStream.setString() PySide.QtCore.QTextStream.device()