PythonQt.Qt.QTextStream
QTextStream
PythonQtInstanceWrapper object
Native bases: PythonQtInstanceWrapper
Supplemental documentation: Qt 5 reference:
Detailed Description
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:
QFile data("output.txt");
if (data.open(QFile::WriteOnly | QFile::Truncate)) {
QTextStream out(&data);
out << "Result: " << qSetFieldWidth(10) << left << 3.14 << 2.7;
// writes "Result: 3.14 2.7 "
}
It's also common to use QTextStream to read console input and write console output. QTextStream is locale aware, and will automatically decode standard input using the correct codec. Example:
QTextStream stream(stdin);
QString line;
while (stream.readLineInto(&line)) {
...
}
Besides using QTextStream's constructors, you can also set the device or string QTextStream operates on by calling setDevice() 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 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, 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:
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:
In addition, Qt provides three global manipulators that take a parameter: qSetFieldWidth(), qSetPadChar(), and qSetRealNumberPrecision().
Source: https://doc.qt.io/archives/qt-5.15/qtextstream.html
Complete member index
Every captured member retains its own identity. Identical captured declaration text is linked once; this does not establish native inheritance or interchangeable objects.
| Member | Kind | Signature | Summary |
|---|---|---|---|
AlignAccountingStyle |
attribute | Any |
(shared captured text) |
AlignCenter |
attribute | Any |
(shared captured text) |
AlignLeft |
attribute | Any |
(shared captured text) |
AlignRight |
attribute | Any |
(shared captured text) |
FieldAlignment |
attribute | type |
Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text) |
FixedNotation |
attribute | Any |
(shared captured text) |
ForcePoint |
attribute | Any |
(shared captured text) |
ForceSign |
attribute | Any |
(shared captured text) |
NumberFlag |
attribute | type |
Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text) |
NumberFlags |
attribute | type |
Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text) |
Ok |
attribute | Any |
(shared captured text) |
ReadCorruptData |
attribute | Any |
(shared captured text) |
ReadPastEnd |
attribute | Any |
(shared captured text) |
RealNumberNotation |
attribute | type |
Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text) |
ScientificNotation |
attribute | Any |
(shared captured text) |
ShowBase |
attribute | Any |
(shared captured text) |
SmartNotation |
attribute | Any |
(shared captured text) |
Status |
attribute | type |
Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text) |
UppercaseBase |
attribute | Any |
(shared captured text) |
UppercaseDigits |
attribute | Any |
(shared captured text) |
WriteFailed |
attribute | Any |
(shared captured text) |
__init__ |
function | (self, *args: Any, **kwargs: Any) -> None |
Native constructor; consult class documentation for supported overloads. (shared captured text) |
atEnd |
function | (self) -> bool |
X.atEnd() -> bool (shared captured text) |
autoDetectUnicode |
function | (self) -> bool |
X.autoDetectUnicode() -> bool (shared captured text) |
className |
function | (self, *args: Any, **kwargs: Any) -> Any |
Return the classname of the object (shared captured text) |
codec |
function | (self) -> Any |
X.codec() -> PythonQt.QtCore.QTextCodec (shared captured text) |
delete |
function | (self, *args: Any, **kwargs: Any) -> Any |
Deletes the C++ object (at your own risk, my friend!) (shared captured text) |
device |
function | (self) -> Any |
X.device() -> PythonQt.QtCore.QIODevice (shared captured text) |
fieldAlignment |
function | (self) -> Any |
Returns the current field alignment. (shared captured text) |
fieldWidth |
function | (self) -> int |
X.fieldWidth() -> int (shared captured text) |
flush |
function | (self) -> None |
Flushes any buffered data waiting to be written to the device. (shared captured text) |
generateByteOrderMark |
function | (self) -> bool |
X.generateByteOrderMark() -> bool (shared captured text) |
help |
function | (self, *args: Any, **kwargs: Any) -> Any |
Shows the help of available methods for this class (shared captured text) |
inherits |
function | (self, *args: Any, **kwargs: Any) -> Any |
Returns if the class inherits or is of given type name (shared captured text) |
integerBase |
function | (self) -> int |
X.integerBase() -> int (shared captured text) |
locale |
function | (self) -> Any |
X.locale() -> PythonQt.QtCore.QLocale (shared captured text) |
numberFlags |
function | (self) -> Any |
Returns the current number flags. (shared captured text) |
padChar |
function | (self) -> str |
Returns the current pad character. (shared captured text) |
pos |
function | (self) -> int |
X.pos() -> int (shared captured text) |
read |
function | (self, maxlen: int) -> str |
X.read(maxlen) -> str (shared captured text) |
readAll |
function | (self) -> str |
X.readAll() -> str (shared captured text) |
readByte |
function | (self, ch: Any) -> Any |
X.readByte(ch) -> PythonQt.QtCore.QTextStream (shared captured text) |
readDouble |
function | (self, f: Any) -> Any |
X.readDouble(f) -> PythonQt.QtCore.QTextStream (shared captured text) |
readFloat |
function | (self, f: Any) -> Any |
X.readFloat(f) -> PythonQt.QtCore.QTextStream (shared captured text) |
readInt |
function | (self, i: Any) -> Any |
X.readInt(i) -> PythonQt.QtCore.QTextStream (shared captured text) |
readLine |
function | (self, maxlen: int) -> str |
X.readLine(maxlen) -> str (shared captured text) |
readLineInto |
function | (self, line: Any, maxlen: Any) -> bool |
X.readLineInto(line, maxlen) -> bool (shared captured text) |
readLongLong |
function | (self, i: Any) -> Any |
X.readLongLong(i) -> PythonQt.QtCore.QTextStream (shared captured text) |
readShort |
function | (self, i: Any) -> Any |
X.readShort(i) -> PythonQt.QtCore.QTextStream (shared captured text) |
realNumberNotation |
function | (self) -> Any |
Returns the current real number notation. (shared captured text) |
realNumberPrecision |
function | (self) -> int |
X.realNumberPrecision() -> int (shared captured text) |
reset |
function | (self) -> None |
Resets [QTextStream](https://doc.qt.io/archives/qt-5.15/qtextstream.html)'s formatting options, bringing it back to its original constructed state. (shared captured text) |
resetStatus |
function | (self) -> None |
Resets the status of the text stream. (shared captured text) |
seek |
function | (self, pos: int) -> bool |
X.seek(pos) -> bool (shared captured text) |
setAutoDetectUnicode |
function | (self, enabled: bool) -> None |
If enabled is true, [QTextStream](https://doc.qt.io/archives/qt-5.15/qtextstream.html) will attempt to detect Unicode encoding by peeking into the stream data to see if it can find the UTF-8,… (shared captured text) |
setCodec |
function | (self, codec: Any) -> None |
Sets the codec for this stream to codec. (shared captured text) |
setDevice |
function | (self, device: Any) -> None |
Sets the current device to device. (shared captured text) |
setFieldAlignment |
function | (self, alignment: Any) -> None |
Sets the field alignment to mode. (shared captured text) |
setFieldWidth |
function | (self, width: int) -> None |
Sets the current field width to width. (shared captured text) |
setGenerateByteOrderMark |
function | (self, generate: bool) -> None |
If generate is true and a UTF codec is used, [QTextStream](https://doc.qt.io/archives/qt-5.15/qtextstream.html) will insert the BOM (Byte Order Mark) before any data has been written to the device. (shared captured text) |
setIntegerBase |
function | (self, base: int) -> None |
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). (shared captured text) |
setLocale |
function | (self, locale: Any) -> None |
Sets the locale for this stream to locale. (shared captured text) |
setNumberFlags |
function | (self, arg0: flags) -> None |
Sets the current number flags to flags. flags is a set of flags from the [NumberFlag](https://doc.qt.io/archives/qt-5.15/qtextstream.html#NumberFlag-enum) enum, and describes options for… (shared captured text) |
setPadChar |
function | (self, ch: str) -> None |
Sets the pad character to ch. (shared captured text) |
setRealNumberNotation |
function | (self, notation: Any) -> None |
Sets the real number notation to notation ([SmartNotation](https://doc.qt.io/archives/qt-5.15/qtextstream.html#RealNumberNotation-enum),… (shared captured text) |
setRealNumberPrecision |
function | (self, precision: int) -> None |
Sets the precision of real numbers to precision. (shared captured text) |
setStatus |
function | (self, status: Any) -> None |
Sets the status of the text stream to the status given. (shared captured text) |
skipWhiteSpace |
function | (self) -> None |
Reads and discards whitespace from the stream until either a non-space character is detected, or until [atEnd](https://doc.qt.io/archives/qt-5.15/qtextstream.html#atEnd)() returns true. (shared captured text) |
status |
function | (self) -> Any |
Returns the status of the text stream. (shared captured text) |
writeByte |
function | (self, ch: Any) -> Any |
X.writeByte(ch) -> PythonQt.QtCore.QTextStream (shared captured text) |
writeDouble |
function | (self, f: Any) -> Any |
X.writeDouble(f) -> PythonQt.QtCore.QTextStream (shared captured text) |
writeFloat |
function | (self, f: Any) -> Any |
X.writeFloat(f) -> PythonQt.QtCore.QTextStream (shared captured text) |
writeInt |
function | (self, i: Any) -> Any |
X.writeInt(i) -> PythonQt.QtCore.QTextStream (shared captured text) |
writeLongLong |
function | (self, i: Any) -> Any |
X.writeLongLong(i) -> PythonQt.QtCore.QTextStream (shared captured text) |
writeShort |
function | (self, i: Any) -> Any |
X.writeShort(i) -> PythonQt.QtCore.QTextStream (shared captured text) |