QDebug 類

The QDebug class provides an output stream for debugging information. 更多...

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

公共函數

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 )
void setVerbosity (int verbosityLevel )
QDebug & space ()
void swap (QDebug & other )
int verbosity () const
QDebug & operator<< (QChar t )
QDebug & operator<< (bool t )
QDebug & operator<< (char t )
QDebug & operator<< (signed short i )
QDebug & operator<< (unsigned short i )
QDebug & operator<< (char16_t t )
QDebug & operator<< (char32_t t )
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 )
QDebug operator<< (QDebug stream , const QList<T> & list )
QDebug operator<< (QDebug stream , const std::list<T, Alloc> & list )
QDebug operator<< (QDebug stream , const QVector<T> & vector )
QDebug operator<< (QDebug stream , const std::vector<T, Alloc> & vector )
QDebug operator<< (QDebug stream , const QSet<T> & set )
QDebug operator<< (QDebug stream , const QMap<Key, T> & map )
QDebug operator<< (QDebug stream , const std::map<Key, T, Compare, Alloc> & map )
QDebug operator<< (QDebug stream , const std::multimap<Key, T, Compare, Alloc> & map )
QDebug operator<< (QDebug stream , const QHash<Key, T> & hash )
QDebug operator<< (QDebug stream , const QPair<T1, T2> & pair )
QDebug operator<< (QDebug stream , const QFlags<T> & flag )

詳細描述

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.

基本用法

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 。同樣, qWarning (), qCritical () 和 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 或任何其它 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.

格式化選項

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

可以微調這些選項透過 space (), nospace () 和 quote (), noquote () 方法。此外, QTextStream 操作符 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.

把自定義類型寫入流

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 調試技術 and 創建自定義 Qt 類型 documents.

成員函數文檔編製

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 )

構造副本為 other debug stream.

QDebug:: ~QDebug ()

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

bool QDebug:: autoInsertSpaces () const

返迴 true 若此 QDebug instance will automatically insert spaces between writes.

該函數在 Qt 5.0 引入。

另請參閱 setAutoInsertSpaces () 和 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.

默認字符是雙引號 " .

該函數在 Qt 5.4 引入。

另請參閱 quote () 和 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.

另請參閱 space () 和 nospace ().

QDebug &QDebug:: noquote ()

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

When quoting is disabled, these types are printed without quotation characters and without escaping of non-printable characters.

該函數在 Qt 5.4 引入。

另請參閱 quote () 和 maybeQuote ().

QDebug &QDebug:: nospace ()

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

另請參閱 space () 和 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.

該函數在 Qt 5.4 引入。

另請參閱 noquote () 和 maybeQuote ().

QDebug &QDebug:: resetFormat ()

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

該函數在 Qt 5.4 引入。

另請參閱 space () 和 quote ().

void QDebug:: setAutoInsertSpaces ( bool b )

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

該函數在 Qt 5.0 引入。

另請參閱 autoInsertSpaces () 和 QDebugStateSaver .

void QDebug:: setVerbosity ( int verbosityLevel )

Sets the verbosity of the stream to verbosityLevel .

允許範圍是從 0 到 7。默認值為 2。

該函數在 Qt 5.6 引入。

另請參閱 verbosity ().

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.

另請參閱 nospace () 和 maybeSpace ().

void QDebug:: swap ( QDebug & other )

Swaps this debug stream instance with other 。此函數非常快且從不失敗。

該函數在 Qt 5.0 引入。

int QDebug:: verbosity () const

返迴調試流的冗餘。

Streaming operators can check the value to decide whether verbose output is desired and print more information depending on the level. Higher values indicate that more information is desired.

允許範圍是從 0 到 7。默認值為 2。

該函數在 Qt 5.6 引入。

另請參閱 setVerbosity ().

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

Writes the character, t , to the stream and returns a reference to the stream. Normally, QDebug prints control characters and non-US-ASCII characters as their C escape sequences or their Unicode value (\u1234). To print non-printable characters without transformation, enable the noquote () functionality, but note that some QDebug backends may not be 8-bit clean and may not be able to represent t .

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

Writes the boolean value, t ,到流並返迴流引用。

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

Writes the character, t ,到流並返迴流引用。

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

Writes the signed short integer, i ,到流並返迴流引用。

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

Writes then unsigned short integer, i ,到流並返迴流引用。

QDebug &QDebug:: operator<< ( char16_t t )

Writes the UTF-16 character, t ,到流並返迴流引用。

該函數在 Qt 5.5 引入。

QDebug &QDebug:: operator<< ( char32_t t )

Writes the UTF-32 character, t ,到流並返迴流引用。

該函數在 Qt 5.5 引入。

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

Writes the signed integer, i ,到流並返迴流引用。

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

Writes then unsigned integer, i ,到流並返迴流引用。

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

Writes the signed long integer, l ,到流並返迴流引用。

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

Writes then unsigned long integer, l ,到流並返迴流引用。

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

Writes the signed 64-bit integer, i ,到流並返迴流引用。

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

Writes then unsigned 64-bit integer, i ,到流並返迴流引用。

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

Writes the 32-bit floating point number, f ,到流並返迴流引用。

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

Writes the 64-bit floating point number, f ,到流並返迴流引用。

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

Writes the '\0'-terminated string, s , to the stream and returns a reference to the stream. The string is never quoted nor transformed to the output, but note that some QDebug backends might not be 8-bit clean.

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

寫入字符串 s , to the stream and returns a reference to the stream. Normally, QDebug prints the string inside quotes and transforms non-printable characters to their Unicode values (\u1234).

To print non-printable characters without transformation, enable the noquote () functionality. Note that some QDebug backends might not be 8-bit clean.

輸齣範例:

QString s;
s = "a";
qDebug().noquote() << s;    // prints: a
qDebug() << s;              // prints: "a"
s = "\"a\r\n\"";
qDebug() << s;              // prints: "\"a\r\n\""
s = "\033";                 // escape character
qDebug() << s;              // prints: "\u001B"
s = "\u00AD";               // SOFT HYPHEN
qDebug() << s;              // prints: "\u00AD"
s = "\u00E1";               // LATIN SMALL LETTER A WITH ACUTE
qDebug() << s;              // prints: "á"
s = "a\u0301";              // "a" followed by COMBINING ACUTE ACCENT
qDebug() << s;              // prints: "á";
s = "\u0430\u0301";         // CYRILLIC SMALL LETTER A followed by COMBINING ACUTE ACCENT
qDebug() << s;              // prints: "а́"
					

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

寫入字符串 s , to the stream and returns a reference to the stream. Normally, QDebug prints the string inside quotes and transforms non-printable characters to their Unicode values (\u1234).

To print non-printable characters without transformation, enable the noquote () functionality. Note that some QDebug backends might not be 8-bit clean.

QString overload for examples.

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

寫入字符串 s , to the stream and returns a reference to the stream. Normally, QDebug prints the string inside quotes and transforms non-printable characters to their Unicode values (\u1234).

To print non-printable characters without transformation, enable the noquote () functionality. Note that some QDebug backends might not be 8-bit clean.

QString overload for examples.

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

Writes the byte array, b , to the stream and returns a reference to the stream. Normally, QDebug prints the array inside quotes and transforms control or non-US-ASCII characters to their C escape sequences (\xAB). This way, the output is always 7-bit clean and the string can be copied from the output and pasted back into C++ sources, if necessary.

To print non-printable characters without transformation, enable the noquote () functionality. Note that some QDebug backends might not be 8-bit clean.

輸齣範例:

QByteArray ba;
ba = "a";
qDebug().noquote() << ba;    // prints: a
qDebug() << ba;              // prints: "a"
ba = "\"a\r\n\"";
qDebug() << ba;              // prints: "\"a\r\n\""
ba = "\033";                 // escape character
qDebug() << ba;              // prints: "\x1B"
ba = "\xC3\xA1";
qDebug() << ba;              // prints: "\xC3\xA1"
ba = QByteArray("a\0b", 3);
qDebug() << ba               // prints: "\a\x00""b"
					

Note how QDebug needed to close and reopen the string in the way C and C++ languages concatenate string literals so that the letter 'b' is not interpreted as part of the previous hexadecimal escape sequence.

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

寫入指針 p ,到流並返迴流引用。

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

賦值 other debug stream to this stream and returns a reference to this stream.

相關非成員

QDebug operator<< ( QDebug stream , const QList < T > & list )

Writes the contents of list to stream . T needs to support streaming into QDebug .

QDebug operator<< ( QDebug stream , const std::list < T , Alloc > & list )

Writes the contents of list to stream . T needs to support streaming into QDebug .

該函數在 Qt 5.7 引入。

QDebug operator<< ( QDebug stream , const QVector < T > & vector )

Writes the contents of vector to stream . T needs to support streaming into QDebug .

QDebug operator<< ( QDebug stream , const std::vector < T , Alloc > & vector )

Writes the contents of vector to stream . T needs to support streaming into QDebug .

該函數在 Qt 5.7 引入。

QDebug operator<< ( QDebug stream , const QSet < T > & set )

Writes the contents of set to stream . T needs to support streaming into QDebug .

QDebug operator<< ( QDebug stream , const QMap < Key , T > & map )

Writes the contents of map to stream . Both Key and T need to support streaming into QDebug .

QDebug operator<< ( QDebug stream , const std::map < Key , T , Compare , Alloc > & map )

Writes the contents of map to stream . Both Key and T need to support streaming into QDebug .

該函數在 Qt 5.7 引入。

QDebug operator<< ( QDebug stream , const std::multimap < Key , T , Compare , Alloc > & map )

Writes the contents of map to stream . Both Key and T need to support streaming into QDebug .

該函數在 Qt 5.7 引入。

QDebug operator<< ( QDebug stream , const QHash < Key , T > & hash )

Writes the contents of hash to stream . Both Key and T need to support streaming into QDebug .

QDebug operator<< ( QDebug stream , const QPair < T1 , T2 > & pair )

Writes the contents of pair to stream . Both T1 and T2 need to support streaming into QDebug .

QDebug operator<< ( QDebug stream , const QFlags < T > & flag )

寫入 flag to stream .

該函數在 Qt 4.7 引入。