QDebug Class

The QDebug class provides an output stream for debugging information. More...

Header: #include <QDebug>
qmake: QT += core
Inherited By:

Public Functions

QDebug(QIODevice * device)
QDebug(QString * string)
QDebug(QtMsgType type)
QDebug(const QDebug & other)
~QDebug()
bool autoInsertSpaces() const
QDebug & maybeQuote(char c = '"')
QDebug & maybeSpace()
QDebug & noquote()
QDebug & nospace()
QDebug & quote()
QDebug & resetFormat()
void setAutoInsertSpaces(bool b)
QDebug & space()
void swap(QDebug & other)
QDebug & operator<<(QChar t)
QDebug & operator<<(bool t)
QDebug & operator<<(char t)
QDebug & operator<<(signed short i)
QDebug & operator<<(unsigned short i)
QDebug & operator<<(signed int i)
QDebug & operator<<(unsigned int i)
QDebug & operator<<(signed long l)
QDebug & operator<<(unsigned long l)
QDebug & operator<<(qint64 i)
QDebug & operator<<(quint64 i)
QDebug & operator<<(float f)
QDebug & operator<<(double f)
QDebug & operator<<(const char * s)
QDebug & operator<<(const QString & s)
QDebug & operator<<(const QStringRef & s)
QDebug & operator<<(QLatin1String s)
QDebug & operator<<(const QByteArray & b)
QDebug & operator<<(const void * p)
QDebug & operator=(const QDebug & other)

Detailed Description

The QDebug class provides an output stream for debugging information.

QDebug is used whenever the developer needs to write out debugging or tracing information to a device, file, string or console.

Basic Use

In the common case, it is useful to call the qDebug() function to obtain a default QDebug object to use for writing debugging information.

    qDebug() << "Date:" << QDate::currentDate();
    qDebug() << "Types:" << QString("String") << QChar('x') << QRect(0, 10, 50, 40);
    qDebug() << "Custom coordinate type:" << coordinate;

This constructs a QDebug object using the constructor that accepts a QtMsgType value of QtDebugMsg. Similarly, the qWarning(), qCritical() and qFatal() functions also return QDebug objects for the corresponding message types.

The class also provides several constructors for other situations, including a constructor that accepts a QFile or any other QIODevice subclass that is used to write debugging information to files and other devices. The constructor that accepts a QString is used to write to a string for display or serialization.

Formatting Options

QDebug formats output so that it's easily readable. It automatically adds spaces between arguments, and adds quotes around QString, QByteArray, QChar arguments.

You can tweak these options through the space(), nospace() and quote(), noquote() methods. Furthermore, QTextStream manipulators can be piped into a QDebug stream.

QDebugStateSaver limits changes to the formatting to the current scope. resetFormat() resets the options to the default ones.

Writing Custom Types to a Stream

Many standard types can be written to QDebug objects, and Qt provides support for most Qt value types. To add support for custom types, you need to implement a streaming operator, as in the following example:

QDebug operator<<(QDebug dbg, const Coordinate &c)
{
    dbg.nospace() << "(" << c.x() << ", " << c.y() << ")";

    return dbg.space();
}

This is described in the Debugging Techniques and Creating Custom Qt Types documents.

Member Function Documentation

QDebug::​QDebug(QIODevice * device)

Constructs a debug stream that writes to the given device.

QDebug::​QDebug(QString * string)

Constructs a debug stream that writes to the given string.

QDebug::​QDebug(QtMsgType type)

Constructs a debug stream that writes to the handler for the message type specified by type.

QDebug::​QDebug(const QDebug & other)

Constructs a copy of the other debug stream.

QDebug::​~QDebug()

Flushes any pending data to be written and destroys the debug stream.

bool QDebug::​autoInsertSpaces() const

Returns true if this QDebug instance will automatically insert spaces between writes.

This function was introduced in Qt 5.0.

See also setAutoInsertSpaces() and QDebugStateSaver.

QDebug & QDebug::​maybeQuote(char c = '"')

Writes a character c to the debug stream, depending on the current setting for automatic insertion of quotes, and returns a reference to the stream.

The default character is a double quote ".

This function was introduced in Qt 5.4.

See also quote() and noquote().

QDebug & QDebug::​maybeSpace()

Writes a space character to the debug stream, depending on the current setting for automatic insertion of spaces, and returns a reference to the stream.

See also space() and nospace().

QDebug & QDebug::​noquote()

Disables automatic insertion of quotation characters around QChar, QString and QByteArray contents and returns a reference to the stream.

This function was introduced in Qt 5.4.

See also quote() and maybeQuote().

QDebug & QDebug::​nospace()

Disables automatic insertion of spaces and returns a reference to the stream.

See also space() and maybeSpace().

QDebug & QDebug::​quote()

Enables automatic insertion of quotation characters around QChar, QString and QByteArray contents and returns a reference to the stream.

Quoting is enabled by default.

This function was introduced in Qt 5.4.

See also noquote() and maybeQuote().

QDebug & QDebug::​resetFormat()

Resets the stream formatting options, bringing it back to its original constructed state.

This function was introduced in Qt 5.4.

See also space() and quote().

void QDebug::​setAutoInsertSpaces(bool b)

Enables automatic insertion of spaces between writes if b is true; otherwise automatic insertion of spaces is disabled.

This function was introduced in Qt 5.0.

See also autoInsertSpaces() and QDebugStateSaver.

QDebug & QDebug::​space()

Writes a space character to the debug stream and returns a reference to the stream.

The stream remembers that automatic insertion of spaces is enabled for future writes.

See also nospace() and maybeSpace().

void QDebug::​swap(QDebug & other)

Swaps this debug stream instance with other. This function is very fast and never fails.

This function was introduced in Qt 5.0.

QDebug & QDebug::​operator<<(QChar t)

Writes the character, t, to the stream and returns a reference to the stream.

QDebug & QDebug::​operator<<(bool t)

Writes the boolean value, t, to the stream and returns a reference to the stream.

QDebug & QDebug::​operator<<(char t)

Writes the character, t, to the stream and returns a reference to the stream.

QDebug & QDebug::​operator<<(signed short i)

Writes the signed short integer, i, to the stream and returns a reference to the stream.

QDebug & QDebug::​operator<<(unsigned short i)

Writes then unsigned short integer, i, to the stream and returns a reference to the stream.

QDebug & QDebug::​operator<<(signed int i)

Writes the signed integer, i, to the stream and returns a reference to the stream.

QDebug & QDebug::​operator<<(unsigned int i)

Writes then unsigned integer, i, to the stream and returns a reference to the stream.

QDebug & QDebug::​operator<<(signed long l)

Writes the signed long integer, l, to the stream and returns a reference to the stream.

QDebug & QDebug::​operator<<(unsigned long l)

Writes then unsigned long integer, l, to the stream and returns a reference to the stream.

QDebug & QDebug::​operator<<(qint64 i)

Writes the signed 64-bit integer, i, to the stream and returns a reference to the stream.

QDebug & QDebug::​operator<<(quint64 i)

Writes then unsigned 64-bit integer, i, to the stream and returns a reference to the stream.

QDebug & QDebug::​operator<<(float f)

Writes the 32-bit floating point number, f, to the stream and returns a reference to the stream.

QDebug & QDebug::​operator<<(double f)

Writes the 64-bit floating point number, f, to the stream and returns a reference to the stream.

QDebug & QDebug::​operator<<(const char * s)

Writes the '\0'-terminated string, s, to the stream and returns a reference to the stream.

QDebug & QDebug::​operator<<(const QString & s)

Writes the string, s, to the stream and returns a reference to the stream.

QDebug & QDebug::​operator<<(const QStringRef & s)

Writes the string reference, s, to the stream and returns a reference to the stream.

QDebug & QDebug::​operator<<(QLatin1String s)

Writes the Latin1-encoded string, s, to the stream and returns a reference to the stream.

QDebug & QDebug::​operator<<(const QByteArray & b)

Writes the byte array, b, to the stream and returns a reference to the stream.

QDebug & QDebug::​operator<<(const void * p)

Writes a pointer, p, to the stream and returns a reference to the stream.

QDebug & QDebug::​operator=(const QDebug & other)

Assigns the other debug stream to this stream and returns a reference to this stream.

© 2015 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.