QDebug Class Reference

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

 #include <QDebug>

Public Functions

QDebug ( QIODevice * device )
QDebug ( QString * string )
QDebug ( QtMsgType type )
QDebug ( const QDebug & other )
~QDebug ()
QDebug & maybeSpace ()
QDebug & nospace ()
QDebug & space ()
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<< ( const 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.

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.

QDebug & QDebug::maybeSpace ()

Writes a space character to the debug stream, depending on the last character sent to the stream, and returns a reference to the stream.

If the last character was a space character, this function writes a space character to the stream; otherwise, no characters are written to the stream.

See also space() and nospace().

QDebug & QDebug::nospace ()

Clears the stream's internal flag that records whether the last character was a space and returns a reference to the stream.

See also space() and maybeSpace().

QDebug & QDebug::space ()

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

The stream will record that the last character sent to the stream was a space.

See also nospace() and maybeSpace().

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

Notes provided by the Qt Community
Informative
  • 5

Votes: 2

Coverage: Qt 4.8

Picture of koahnig koahnig

Gene Splicer
7 notes

Subtle difference in the use of QDebug for your own class

The casual reader may assume that the class is simply another way of implementing some sort of an output stream. Well, the use in your implementation is slightly different or at least it has to be for some compilers.

At first an example parameter list for overwriting an std::ostream can be found in the Internet e.g. here [cplusplus.com]

  1. ostream& operator<< (short val)

The different versions show a reference of the output stream as a return value and also some parameter lists show an ostream &.
This looks quite similar to the definition of the output operators for some standard types (e.g.) [qt-project.org]

  1. QDebug & QDebug::operator<< ( signed short i )

Casual reading of the documentation, and the thinking of the handling is identical, may lead to following implementation:

  1. #include <QtCore/QCoreApplication>
  2. #include <QDebug>
  3.  
  4. class MyClassA
  5. {
  6.     int A1;
  7.     int A2;
  8. public:
  9.     MyClassA ( int i1, int i2 )
  10.         : A1 ( i1 )
  11.         , A2 ( i2 ) {}
  12.     int getA1 () const {return A1;};
  13.     int getA2 () const {return A2;};
  14. };
  15.  
  16. QDebug & operator<< ( QDebug & os, const MyClassA & val )
  17. {
  18.     os << val.getA1() << " " << val.getA2();
  19.     return os;
  20. }
  21.  
  22. int main(int argc, char *argv[])
  23. {
  24.     QCoreApplication a(argc, argv);
  25.     MyClassA aval( 1, 12 );
  26.  
  27.     qDebug() << aval << " " << aval;
  28.     return a.exec();
  29. }

At least some compilers will compile without a note and the application will function as supposed. Among those compilers is the one of MS VC 2005.

Porting to minGW will unveil a surprise. A compile error is displayed and the compilation is stopped.
Modifying

  1. QDebug & operator<< ( QDebug & os, const MyClassA & val )

to
  1. QDebug operator<< ( QDebug os, const MyClassA & val )

solves finally the problem. The second line follows the example given in the documentation:

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:
  1. QDebug operator<<(QDebug dbg, const Coordinate &c)
  2.  {
  3.      dbg.nospace() << "(" << c.x() << ", " << c.y() << ")";
  4.  
  5.      return dbg.space();
  6.  }

[Revisions]

Cool Hack
  • 4

Votes: 3

Coverage: Qt library 4.7, 4.8, qt 5.0

Picture of Sam Sam

Ant Farmer
19 notes

To restrict qDebug() output to a new line.

In order to restrict qDebug to append to a newline we can use

  1. QDebug debugMessage(QtDebugMsg);
  2. debugMessage << "Hello";
  3. debugMessage << "World";

The newline will be appended as soon as the debugMessage object goes out of scope.

reference [qt-project.org]

[Revisions]