Qt 4.8
Classes | Public Functions | Properties | List of all members
QDebug Class Reference

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

#include <qdebug.h>

Inheritance diagram for QDebug:
QDeclarativeInfo

Classes

struct  Stream
 

Public Functions

QDebugmaybeSpace ()
 Writes a space character to the debug stream, depending on the last character sent to the stream, and returns a reference to the stream. More...
 
QDebugnospace ()
 Clears the stream's internal flag that records whether the last character was a space and returns a reference to the stream. More...
 
QDebugoperator<< (QChar t)
 Writes the character, t, to the stream and returns a reference to the stream. More...
 
QDebugoperator<< (QBool t)
 Writes the boolean value, t, to the stream and returns a reference to the stream. More...
 
QDebugoperator<< (bool t)
 Writes the boolean value, t, to the stream and returns a reference to the stream. More...
 
QDebugoperator<< (char t)
 Writes the character, t, to the stream and returns a reference to the stream. More...
 
QDebugoperator<< (signed short t)
 Writes the signed short integer, i, to the stream and returns a reference to the stream. More...
 
QDebugoperator<< (unsigned short t)
 Writes then unsigned short integer, i, to the stream and returns a reference to the stream. More...
 
QDebugoperator<< (signed int t)
 Writes the signed integer, i, to the stream and returns a reference to the stream. More...
 
QDebugoperator<< (unsigned int t)
 Writes then unsigned integer, i, to the stream and returns a reference to the stream. More...
 
QDebugoperator<< (signed long t)
 Writes the signed long integer, l, to the stream and returns a reference to the stream. More...
 
QDebugoperator<< (unsigned long t)
 Writes then unsigned long integer, l, to the stream and returns a reference to the stream. More...
 
QDebugoperator<< (qint64 t)
 Writes the signed 64-bit integer, i, to the stream and returns a reference to the stream. More...
 
QDebugoperator<< (quint64 t)
 Writes then unsigned 64-bit integer, i, to the stream and returns a reference to the stream. More...
 
QDebugoperator<< (float t)
 Writes the 32-bit floating point number, f, to the stream and returns a reference to the stream. More...
 
QDebugoperator<< (double t)
 Writes the 64-bit floating point number, f, to the stream and returns a reference to the stream. More...
 
QDebugoperator<< (const char *t)
 Writes the '\0'-terminated string, s, to the stream and returns a reference to the stream. More...
 
QDebugoperator<< (const QString &t)
 Writes the string, s, to the stream and returns a reference to the stream. More...
 
QDebugoperator<< (const QStringRef &t)
 Writes the string reference, s, to the stream and returns a reference to the stream. More...
 
QDebugoperator<< (const QLatin1String &t)
 Writes the Latin1-encoded string, s, to the stream and returns a reference to the stream. More...
 
QDebugoperator<< (const QByteArray &t)
 Writes the byte array, b, to the stream and returns a reference to the stream. More...
 
QDebugoperator<< (const void *t)
 Writes a pointer, p, to the stream and returns a reference to the stream. More...
 
QDebugoperator<< (QTextStreamFunction f)
 
QDebugoperator<< (QTextStreamManipulator m)
 
QDebugoperator= (const QDebug &other)
 Assigns the other debug stream to this stream and returns a reference to this stream. More...
 
 QDebug (QIODevice *device)
 Constructs a debug stream that writes to the given device. More...
 
 QDebug (QString *string)
 Constructs a debug stream that writes to the given string. More...
 
 QDebug (QtMsgType t)
 Constructs a debug stream that writes to the handler for the message type specified by type. More...
 
 QDebug (const QDebug &o)
 Constructs a copy of the other debug stream. More...
 
QDebugspace ()
 Writes a space character to the debug stream and returns a reference to the stream. More...
 
 ~QDebug ()
 Flushes any pending data to be written and destroys the debug stream. More...
 

Properties

struct QDebug::Streamstream
 

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::Making the Type Printable{Creating Custom Qt Types} documents.

Definition at line 62 of file qdebug.h.

Constructors and Destructors

◆ QDebug() [1/4]

QDebug::QDebug ( QIODevice device)
inline

Constructs a debug stream that writes to the given device.

Definition at line 76 of file qdebug.h.

76 : stream(new Stream(device)) {}
struct QDebug::Stream * stream

◆ QDebug() [2/4]

QDebug::QDebug ( QString string)
inline

Constructs a debug stream that writes to the given string.

Definition at line 77 of file qdebug.h.

77 : stream(new Stream(string)) {}
struct QDebug::Stream * stream

◆ QDebug() [3/4]

QDebug::QDebug ( QtMsgType  t)
inline

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

Definition at line 78 of file qdebug.h.

78 : stream(new Stream(t)) {}
struct QDebug::Stream * stream

◆ QDebug() [4/4]

QDebug::QDebug ( const QDebug o)
inline

Constructs a copy of the other debug stream.

Definition at line 79 of file qdebug.h.

79 :stream(o.stream) { ++stream->ref; }
struct QDebug::Stream * stream

◆ ~QDebug()

QDebug::~QDebug ( )
inline

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

Definition at line 81 of file qdebug.h.

81  {
82  if (!--stream->ref) {
83  if(stream->message_output) {
84  QT_TRY {
86  } QT_CATCH(std::bad_alloc&) { /* We're out of memory - give up. */ }
87  }
88  delete stream;
89  }
90  }
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
QString buffer
Definition: qdebug.h:69
#define QT_CATCH(A)
Definition: qglobal.h:1537
QByteArray toLocal8Bit() const Q_REQUIRED_RESULT
Returns the local 8-bit representation of the string as a QByteArray.
Definition: qstring.cpp:4049
QtMsgType type
Definition: qdebug.h:71
void qt_message_output(QtMsgType msgType, const char *buf)
Definition: qglobal.cpp:2712
bool message_output
Definition: qdebug.h:73
struct QDebug::Stream * stream
#define QT_TRY
Definition: qglobal.h:1536

Functions

◆ maybeSpace()

QDebug & QDebug::maybeSpace ( )
inline

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(), nospace()

Definition at line 93 of file qdebug.h.

Referenced by operator<<().

93 { if (stream->space) stream->ts << ' '; return *this; }
bool space
Definition: qdebug.h:72
struct QDebug::Stream * stream
QTextStream ts
Definition: qdebug.h:68

◆ nospace()

QDebug & QDebug::nospace ( )
inline

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(), maybeSpace()

Definition at line 92 of file qdebug.h.

Referenced by debugVariantList(), flagsDebug(), operator<<(), QDeclarativeInfo::QDeclarativeInfo(), and streamDebug().

92 { stream->space = false; return *this; }
bool space
Definition: qdebug.h:72
struct QDebug::Stream * stream

◆ operator<<() [1/22]

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

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

Definition at line 95 of file qdebug.h.

Referenced by QDeclarativeInfo::operator<<().

95 { stream->ts << '\'' << t << '\''; return maybeSpace(); }
QDebug & maybeSpace()
Writes a space character to the debug stream, depending on the last character sent to the stream...
Definition: qdebug.h:93
struct QDebug::Stream * stream
QTextStream ts
Definition: qdebug.h:68

◆ operator<<() [2/22]

QDebug & QDebug::operator<< ( QBool  t)
inline

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

Warning
This function is not part of the public interface.

Definition at line 96 of file qdebug.h.

96 { stream->ts << (bool(t != 0) ? "true" : "false"); return maybeSpace(); }
QDebug & maybeSpace()
Writes a space character to the debug stream, depending on the last character sent to the stream...
Definition: qdebug.h:93
struct QDebug::Stream * stream
QTextStream ts
Definition: qdebug.h:68

◆ operator<<() [3/22]

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

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

Definition at line 97 of file qdebug.h.

97 { stream->ts << (t ? "true" : "false"); return maybeSpace(); }
QDebug & maybeSpace()
Writes a space character to the debug stream, depending on the last character sent to the stream...
Definition: qdebug.h:93
struct QDebug::Stream * stream
QTextStream ts
Definition: qdebug.h:68

◆ operator<<() [4/22]

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

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

Definition at line 98 of file qdebug.h.

98 { stream->ts << t; return maybeSpace(); }
QDebug & maybeSpace()
Writes a space character to the debug stream, depending on the last character sent to the stream...
Definition: qdebug.h:93
struct QDebug::Stream * stream
QTextStream ts
Definition: qdebug.h:68

◆ operator<<() [5/22]

QDebug & QDebug::operator<< ( signed short  t)
inline

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

Definition at line 99 of file qdebug.h.

99 { stream->ts << t; return maybeSpace(); }
QDebug & maybeSpace()
Writes a space character to the debug stream, depending on the last character sent to the stream...
Definition: qdebug.h:93
struct QDebug::Stream * stream
QTextStream ts
Definition: qdebug.h:68

◆ operator<<() [6/22]

QDebug & QDebug::operator<< ( unsigned short  t)
inline

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

Definition at line 100 of file qdebug.h.

100 { stream->ts << t; return maybeSpace(); }
QDebug & maybeSpace()
Writes a space character to the debug stream, depending on the last character sent to the stream...
Definition: qdebug.h:93
struct QDebug::Stream * stream
QTextStream ts
Definition: qdebug.h:68

◆ operator<<() [7/22]

QDebug & QDebug::operator<< ( signed int  t)
inline

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

Definition at line 101 of file qdebug.h.

101 { stream->ts << t; return maybeSpace(); }
QDebug & maybeSpace()
Writes a space character to the debug stream, depending on the last character sent to the stream...
Definition: qdebug.h:93
struct QDebug::Stream * stream
QTextStream ts
Definition: qdebug.h:68

◆ operator<<() [8/22]

QDebug & QDebug::operator<< ( unsigned int  t)
inline

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

Definition at line 102 of file qdebug.h.

102 { stream->ts << t; return maybeSpace(); }
QDebug & maybeSpace()
Writes a space character to the debug stream, depending on the last character sent to the stream...
Definition: qdebug.h:93
struct QDebug::Stream * stream
QTextStream ts
Definition: qdebug.h:68

◆ operator<<() [9/22]

QDebug & QDebug::operator<< ( signed long  t)
inline

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

Definition at line 103 of file qdebug.h.

103 { stream->ts << t; return maybeSpace(); }
QDebug & maybeSpace()
Writes a space character to the debug stream, depending on the last character sent to the stream...
Definition: qdebug.h:93
struct QDebug::Stream * stream
QTextStream ts
Definition: qdebug.h:68

◆ operator<<() [10/22]

QDebug & QDebug::operator<< ( unsigned long  t)
inline

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

Definition at line 104 of file qdebug.h.

104 { stream->ts << t; return maybeSpace(); }
QDebug & maybeSpace()
Writes a space character to the debug stream, depending on the last character sent to the stream...
Definition: qdebug.h:93
struct QDebug::Stream * stream
QTextStream ts
Definition: qdebug.h:68

◆ operator<<() [11/22]

QDebug & QDebug::operator<< ( qint64  t)
inline

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

Definition at line 105 of file qdebug.h.

106  { stream->ts << QString::number(t); return maybeSpace(); }
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
QDebug & maybeSpace()
Writes a space character to the debug stream, depending on the last character sent to the stream...
Definition: qdebug.h:93
struct QDebug::Stream * stream
QTextStream ts
Definition: qdebug.h:68

◆ operator<<() [12/22]

QDebug & QDebug::operator<< ( quint64  t)
inline

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

Definition at line 107 of file qdebug.h.

108  { stream->ts << QString::number(t); return maybeSpace(); }
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
QDebug & maybeSpace()
Writes a space character to the debug stream, depending on the last character sent to the stream...
Definition: qdebug.h:93
struct QDebug::Stream * stream
QTextStream ts
Definition: qdebug.h:68

◆ operator<<() [13/22]

QDebug & QDebug::operator<< ( float  t)
inline

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

Definition at line 109 of file qdebug.h.

109 { stream->ts << t; return maybeSpace(); }
QDebug & maybeSpace()
Writes a space character to the debug stream, depending on the last character sent to the stream...
Definition: qdebug.h:93
struct QDebug::Stream * stream
QTextStream ts
Definition: qdebug.h:68

◆ operator<<() [14/22]

QDebug & QDebug::operator<< ( double  t)
inline

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

Definition at line 110 of file qdebug.h.

110 { stream->ts << t; return maybeSpace(); }
QDebug & maybeSpace()
Writes a space character to the debug stream, depending on the last character sent to the stream...
Definition: qdebug.h:93
struct QDebug::Stream * stream
QTextStream ts
Definition: qdebug.h:68

◆ operator<<() [15/22]

QDebug & QDebug::operator<< ( const char *  t)
inline

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

Definition at line 111 of file qdebug.h.

111 { stream->ts << QString::fromAscii(t); return maybeSpace(); }
static QString fromAscii(const char *, int size=-1)
Returns a QString initialized with the first size characters from the string str. ...
Definition: qstring.cpp:4276
QDebug & maybeSpace()
Writes a space character to the debug stream, depending on the last character sent to the stream...
Definition: qdebug.h:93
struct QDebug::Stream * stream
QTextStream ts
Definition: qdebug.h:68

◆ operator<<() [16/22]

QDebug & QDebug::operator<< ( const QString t)
inline

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

Definition at line 112 of file qdebug.h.

112 { stream->ts << '\"' << t << '\"'; return maybeSpace(); }
QDebug & maybeSpace()
Writes a space character to the debug stream, depending on the last character sent to the stream...
Definition: qdebug.h:93
struct QDebug::Stream * stream
QTextStream ts
Definition: qdebug.h:68

◆ operator<<() [17/22]

QDebug & QDebug::operator<< ( const QStringRef t)
inline

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

Definition at line 113 of file qdebug.h.

Referenced by operator<<().

113 { return operator<<(t.toString()); }
QString toString() const
Returns a copy of the string reference as a QString object.
Definition: qstring.cpp:8653
QDebug & operator<<(QChar t)
Writes the character, t, to the stream and returns a reference to the stream.
Definition: qdebug.h:95

◆ operator<<() [18/22]

QDebug & QDebug::operator<< ( const QLatin1String t)
inline

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

Definition at line 114 of file qdebug.h.

114 { stream->ts << '\"' << t.latin1() << '\"'; return maybeSpace(); }
const char * latin1() const
Returns the Latin-1 string stored in this object.
Definition: qstring.h:661
QDebug & maybeSpace()
Writes a space character to the debug stream, depending on the last character sent to the stream...
Definition: qdebug.h:93
struct QDebug::Stream * stream
QTextStream ts
Definition: qdebug.h:68

◆ operator<<() [19/22]

QDebug & QDebug::operator<< ( const QByteArray t)
inline

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

Definition at line 115 of file qdebug.h.

115 { stream->ts << '\"' << t << '\"'; return maybeSpace(); }
QDebug & maybeSpace()
Writes a space character to the debug stream, depending on the last character sent to the stream...
Definition: qdebug.h:93
struct QDebug::Stream * stream
QTextStream ts
Definition: qdebug.h:68

◆ operator<<() [20/22]

QDebug & QDebug::operator<< ( const void *  t)
inline

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

Definition at line 116 of file qdebug.h.

116 { stream->ts << t; return maybeSpace(); }
QDebug & maybeSpace()
Writes a space character to the debug stream, depending on the last character sent to the stream...
Definition: qdebug.h:93
struct QDebug::Stream * stream
QTextStream ts
Definition: qdebug.h:68

◆ operator<<() [21/22]

QDebug & QDebug::operator<< ( QTextStreamFunction  f)
inline
Warning
This function is not part of the public interface.

Definition at line 117 of file qdebug.h.

117  {
118  stream->ts << f;
119  return *this;
120  }
struct QDebug::Stream * stream
QTextStream ts
Definition: qdebug.h:68

◆ operator<<() [22/22]

QDebug & QDebug::operator<< ( QTextStreamManipulator  m)
inline
Warning
This function is not part of the public interface.

Definition at line 122 of file qdebug.h.

123  { stream->ts << m; return *this; }
struct QDebug::Stream * stream
QTextStream ts
Definition: qdebug.h:68

◆ operator=()

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

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

Definition at line 146 of file qdebug.h.

147 {
148  if (this != &other) {
149  QDebug copy(other);
150  qSwap(stream, copy.stream);
151  }
152  return *this;
153 }
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:62
void qSwap(T &value1, T &value2)
Definition: qglobal.h:2181
struct QDebug::Stream * stream

◆ space()

QDebug & QDebug::space ( )
inline

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(), maybeSpace()

Definition at line 91 of file qdebug.h.

Referenced by operator<<().

91 { stream->space = true; stream->ts << ' '; return *this; }
bool space
Definition: qdebug.h:72
struct QDebug::Stream * stream
QTextStream ts
Definition: qdebug.h:68

Properties

◆ stream

struct QDebug::Stream * QDebug::stream
private

Referenced by operator=().


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