Qx v0.5.8
Qt Extensions Library
Loading...
Searching...
No Matches
Qx::TextStreamWriter Class Reference

The TextStreamWriter class is a specialized wrapper for QTextStream that narrows and simplifies its usage for writing text files. More...

#include <qx/io/qx-textstreamwriter.h>

Public Member Functions

 TextStreamWriter (const QString &filePath, WriteMode writeMode=Append, WriteOptions writeOptions=NoWriteOptions)
 
 TextStreamWriter (WriteMode writeMode=Append, WriteOptions writeOptions=NoWriteOptions)
 
 ~TextStreamWriter ()
 
void closeFile ()
 
QStringConverter::Encoding encoding () const
 
QTextStream::FieldAlignment fieldAlignment () const
 
int fieldWidth () const
 
const QFilefile () const
 
bool fileIsOpen () const
 
QString filePath () const
 
void flush ()
 
bool generateByteOrderMark () const
 
bool hasError () const
 
int integerBase () const
 
QLocale locale () const
 
QTextStream::NumberFlags numberFlags () const
 
IoOpReport openFile ()
 
template<typename T >
requires defines_left_shift_for<QTextStream, T>
TextStreamWriteroperator<< (T d)
 
QChar padChar () const
 
QTextStream::RealNumberNotation realNumberNotation () const
 
int realNumberPrecision () const
 
void reset ()
 
void resetStatus ()
 
void setEncoding (QStringConverter::Encoding encoding)
 
void setFieldAlignment (QTextStream::FieldAlignment mode)
 
void setFieldWidth (int width)
 
void setFilePath (const QString &filePath)
 
void setGenerateByteOrderMark (bool generate)
 
void setIntegerBase (int base)
 
void setLocale (const QLocale &locale)
 
void setNumberFlags (QTextStream::NumberFlags flags)
 
void setPadChar (QChar ch)
 
void setRealNumberNotation (QTextStream::RealNumberNotation notation)
 
void setRealNumberPrecision (int precision)
 
IoOpReport status () const
 
IoOpReport writeLine (QString line, bool ensureLineStart=true)
 
IoOpReport writeText (QString text)
 

Detailed Description

Most member functions are the same or slightly modified versions of those from QTextStream.

The file on which to operate is specified as a path and the underlying handle is managed by the stream.

See also
TextStreamReader and FileStreamWriter

Constructor & Destructor Documentation

◆ TextStreamWriter() [1/2]

Qx::TextStreamWriter::TextStreamWriter ( WriteMode writeMode = Append,
WriteOptions writeOptions = NoWriteOptions )

Constructs a text stream writer that is configured with writeMode and writeOptions.

No file is initially set.

Note
The following WriteMode values are not supported with this class and will be remapped as shown:
See also
setFilePath().

◆ TextStreamWriter() [2/2]

Qx::TextStreamWriter::TextStreamWriter ( const QString & filePath,
WriteMode writeMode = Append,
WriteOptions writeOptions = NoWriteOptions )

Constructs a text stream writer that is linked to the file at filePath, configured with writeMode and writeOptions.

Note
The following WriteMode values are not supported with this class and will be remapped as shown:
See also
filePath() and setFilePath().

◆ ~TextStreamWriter()

Qx::TextStreamWriter::~TextStreamWriter ( )

Destroys the text stream writer, along with closing and deleting the underlying file, if present.

Member Function Documentation

◆ closeFile()

void Qx::TextStreamWriter::closeFile ( )

Closes the file associated with the text stream writer, if present.

◆ encoding()

QStringConverter::Encoding Qx::TextStreamWriter::encoding ( ) const

Returns the encoding that is current assigned to the stream.

See also
setEncoding(), and locale().

◆ fieldAlignment()

QTextStream::FieldAlignment Qx::TextStreamWriter::fieldAlignment ( ) const

Returns the current field alignment.

See also
setFieldAlignment() and fieldWidth().

◆ fieldWidth()

int Qx::TextStreamWriter::fieldWidth ( ) const

Returns the current field width.

See also
setFieldWidth().

◆ file()

const QFile * Qx::TextStreamWriter::file ( ) const

Returns an immutable pointer to the file managed by the stream.

See also
filePath().

◆ fileIsOpen()

bool Qx::TextStreamWriter::fileIsOpen ( ) const

Returns true if the file managed by the stream is open; otherwise, returns false.

◆ filePath()

QString Qx::TextStreamWriter::filePath ( ) const

Returns the path of the file associated with the stream, if present.

If no file is assigned the path will be null.

See also
setFilePath().

◆ flush()

void Qx::TextStreamWriter::flush ( )

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

If the stream was constructed with WriteOption::Unbuffered, this function does nothing.

◆ generateByteOrderMark()

bool Qx::TextStreamWriter::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().

◆ hasError()

bool Qx::TextStreamWriter::hasError ( ) const

Returns true if the stream's current status indicates that an error has occurred; otherwise, returns false.

Equivalent to status().isFailure().

◆ integerBase()

int Qx::TextStreamWriter::integerBase ( ) const

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

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

◆ locale()

QLocale Qx::TextStreamWriter::locale ( ) const

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

See also
setLocale().

◆ numberFlags()

QTextStream::NumberFlags Qx::TextStreamWriter::numberFlags ( ) const

Returns the current number flags.

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

◆ openFile()

IoOpReport Qx::TextStreamWriter::openFile ( )

Opens the file associated with the text stream writer and returns an operation report.

This function must be called before any data is written after a file is assigned to the stream.

◆ operator<<()

template<typename T >
requires defines_left_shift_for<QTextStream, T>
TextStreamWriter & Qx::TextStreamWriter::operator<< ( T d)
inline

Writes d of type T to the stream. Returns a reference to the stream.

This template is constrained such that effectively, the insertion operator for this class is available for all data types that QTextStream defines an insertion operator for.

◆ padChar()

QChar Qx::TextStreamWriter::padChar ( ) const

Returns the current pad character.

See also
setPadChar() and setFieldWidth().

◆ realNumberNotation()

QTextStream::RealNumberNotation Qx::TextStreamWriter::realNumberNotation ( ) const

Returns the current real number notation.

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

◆ realNumberPrecision()

int Qx::TextStreamWriter::realNumberPrecision ( ) const

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

See also
setRealNumberPrecision(), setRealNumberNotation(), realNumberNotation(), numberFlags(), and integerBase().

◆ reset()

void Qx::TextStreamWriter::reset ( )

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

◆ resetStatus()

void Qx::TextStreamWriter::resetStatus ( )

Resets the status of the text stream reader.

Note
If an error occurs while writing the stream will ignore all further write attempts and hold its current status until this function is called.
See also
status().

◆ setEncoding()

void Qx::TextStreamWriter::setEncoding ( QStringConverter::Encoding encoding)

Sets the encoding for this stream to encoding. The encoding is used for any data that is written. By default, QStringConverter::Utf8 is used.

See also
encoding(), and setLocale().

◆ setFieldAlignment()

void Qx::TextStreamWriter::setFieldAlignment ( QTextStream::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() and setFieldWidth().

◆ setFieldWidth()

void Qx::TextStreamWriter::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() and setPadChar().

◆ setFilePath()

void Qx::TextStreamWriter::setFilePath ( const QString & filePath)

Links the stream to the file at filePath, which can be a null QString to unset the current file. If a file was already set to the stream, it will be closed as it is replaced.

The file must be opened through the stream before it can be used.

See also
filePath() and openFile().

◆ setGenerateByteOrderMark()

void Qx::TextStreamWriter::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().

◆ setIntegerBase()

void Qx::TextStreamWriter::setIntegerBase ( int base)

Sets the base of integers to base. base can be either 2 (binary), 8 (octal), 10 (decimal) or 16 (hexadecimal). QTextStream assumes base is 10 unless the base has been set explicitly.

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

◆ setLocale()

void Qx::TextStreamWriter::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().

◆ setNumberFlags()

void Qx::TextStreamWriter::setNumberFlags ( QTextStream::NumberFlags flags)

Sets the current number flags to flags. flags is a set of flags from the QTextStream::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(), and setRealNumberNotation().

◆ setPadChar()

void Qx::TextStreamWriter::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.

See also
padChar(), setFieldWidth() and QTextStream::setPadChar().

◆ setRealNumberNotation()

void Qx::TextStreamWriter::setRealNumberNotation ( QTextStream::RealNumberNotation notation)

Sets the real number notation to notation. When generating numbers, TextStreamWriter uses this value to detect the formatting of real numbers.

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

◆ setRealNumberPrecision()

void Qx::TextStreamWriter::setRealNumberPrecision ( int precision)

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

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

See also
realNumberPrecision() and setRealNumberNotation().

◆ status()

IoOpReport Qx::TextStreamWriter::status ( ) const

Returns the status of the text stream writer.

The status is a report of the last write operation performed by TextStreamWriter. If no write operation has been performed since the stream was constructed or resetStatus() was last called the report will be null.

◆ writeLine()

IoOpReport Qx::TextStreamWriter::writeLine ( QString line,
bool ensureLineStart = true )

Writes line to the stream followed by a line break and returns the streams status.

If ensureLineStart is true, a line break will be written before writing line if the stream wasn't already positioned at the start of a new line.

◆ writeText()

IoOpReport Qx::TextStreamWriter::writeText ( QString text)

Writes text to the stream and returns the stream's status.


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