QDebug Class

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

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

Public Types

enum VerbosityLevel { MinimumVerbosity, DefaultVerbosity, MaximumVerbosity }

Public Functions

QDebug &resetFormat()

Detailed Description

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 debug, const Coordinate &c)
  {
      QDebugStateSaver saver(debug);
      debug.nospace() << '(' << c.x() << ", " << c.y() << ')';

      return debug;
  }

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

Member Type Documentation

enum QDebug::VerbosityLevel

This enum describes the range of verbosity levels.

ConstantValue
QDebug::MinimumVerbosity0
QDebug::DefaultVerbosity2
QDebug::MaximumVerbosity7

This enum was introduced or modified in Qt 5.13.

See also verbosity() and setVerbosity().

Member Function Documentation

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