本页内容

QDebug Class

QDebug 类提供了一个用于调试信息的输出流。更多内容...

头文件: #include <QDebug>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
继承自: QIODeviceBase
被继承者:

QQmlInfo

公共类型

enum VerbosityLevel { MinimumVerbosity, DefaultVerbosity, MaximumVerbosity }

公共函数

(since 6.9) QDebug(QByteArray *byteArray)
QDebug(QIODevice *device)
QDebug(QString *string)
QDebug(QtMsgType t)
QDebug(const QDebug &o)
~QDebug()
bool autoInsertSpaces() const
QDebug &maybeQuote(char c = '"')
QDebug &maybeSpace()
QDebug &noquote()
QDebug &nospace()
QDebug &quote()
(since 6.7) bool quoteStrings() const
QDebug &resetFormat()
void setAutoInsertSpaces(bool b)
(since 6.7) void setQuoteStrings(bool b)
void setVerbosity(int verbosityLevel)
QDebug &space()
void swap(QDebug &other)
int verbosity() const
QDebug &verbosity(int verbosityLevel)
(since 6.0) QDebug &operator<<(QByteArrayView t)
QDebug &operator<<(QChar t)
QDebug &operator<<(QLatin1StringView t)
QDebug &operator<<(QStringView s)
(since 6.0) QDebug &operator<<(QUtf8StringView s)
(since 6.7) QDebug &operator<<(T i)
(since 6.7) QDebug &operator<<(T i)
QDebug &operator<<(bool t)
QDebug &operator<<(char t)
QDebug &operator<<(char16_t t)
QDebug &operator<<(char32_t t)
QDebug &operator<<(const QByteArray &t)
QDebug &operator<<(const QString &t)
QDebug &operator<<(const char *t)
(since 6.0) QDebug &operator<<(const char16_t *t)
(since 6.5) QDebug &operator<<(const std::basic_string<Char, Args...> &s)
(since 6.7) QDebug &operator<<(const std::optional<T> &opt)
(since 6.9) QDebug &operator<<(const std::tuple<Ts...> &tuple)
QDebug &operator<<(const void *t)
QDebug &operator<<(double t)
QDebug &operator<<(float t)
QDebug &operator<<(int t)
QDebug &operator<<(long t)
QDebug &operator<<(qint64 t)
QDebug &operator<<(quint64 t)
QDebug &operator<<(short t)
(since 6.5) QDebug &operator<<(std::basic_string_view<Char, Args...> s)
(since 6.6) QDebug &operator<<(std::chrono::duration<Rep, Period> duration)
(since 6.7) QDebug &operator<<(std::nullopt_t)
QDebug &operator<<(unsigned int t)
QDebug &operator<<(unsigned long t)
QDebug &operator<<(unsigned short t)
QDebug &operator=(const QDebug &other)

静态公共成员

(since 6.9) QByteArray toBytes(const T &object)
(since 6.0) QString toString(const T &object)
QDebug operator<<(QDebug debug, const QHash<Key, T> &hash)
QDebug operator<<(QDebug debug, const QList<T> &list)
QDebug operator<<(QDebug debug, const QMap<Key, T> &map)
QDebug operator<<(QDebug debug, const QMultiHash<Key, T> &hash)
QDebug operator<<(QDebug debug, const QMultiMap<Key, T> &map)
QDebug operator<<(QDebug debug, const QSet<T> &set)
(since 6.3) QDebug operator<<(QDebug debug, const QVarLengthArray<T, P> &array)
(since 6.9) QDebug operator<<(QDebug debug, const std::array<T, N> &array)
QDebug operator<<(QDebug debug, const std::list<T, Alloc> &vec)
QDebug operator<<(QDebug debug, const std::map<Key, T, Compare, Alloc> &map)
QDebug operator<<(QDebug debug, const std::multimap<Key, T, Compare, Alloc> &map)
(since 6.9) QDebug operator<<(QDebug debug, const std::multiset<Key, Compare, Alloc> &multiset)
QDebug operator<<(QDebug debug, const std::pair<T1, T2> &pair)
(since 6.9) QDebug operator<<(QDebug debug, const std::set<Key, Compare, Alloc> &set)
(since 6.9) QDebug operator<<(QDebug debug, const std::unordered_map<Key, T, Hash, KeyEqual, Alloc> &map)
(since 6.9) QDebug operator<<(QDebug debug, const std::unordered_set<Key, Hash, KeyEqual, Alloc> &unordered_set)
QDebug operator<<(QDebug debug, const std::vector<T, Alloc> &vec)
(since 6.7) QDebug operator<<(QDebug d, QAnyStringView s)
(since 6.9) QDebug operator<<(QDebug debug, T t)
QDebug operator<<(QDebug debug, const QContiguousCache<T> &cache)
QDebug operator<<(QDebug debug, const QFlags<T> &flags)

宏

QDebug qCritical()
QDebug qDebug()
QDebug qFatal()
QDebug qInfo()
QDebug qWarning()

详细说明

每当开发人员需要将调试或跟踪信息写入设备、文件、字符串或控制台时,都会使用 QDebug。

基本用法

在常见情况下,调用qDebug()函数以获取一个默认的QDebug对象,用于写入调试信息,会非常有用。

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

这通过接受QtMsgType 值QtDebugMsg 的构造函数来创建一个QDebug对象。同样地,qInfo()、qWarning()、qCritical()和qFatal()函数也会返回对应消息类型的QDebug对象。

该类还提供了几个用于其他情况的构造函数,包括一个可接受QFile 或任何其他QIODevice 子类的构造函数,用于将调试信息写入文件或其他设备。可接受QString 的构造函数用于将内容写入字符串以供显示或序列化。

格式化选项

QDebug 会对输出进行格式化,使其易于阅读。它会在参数之间自动添加空格,并在QString 、QByteArray 以及QChar 参数周围添加引号。

您可以通过space()、nospace()、quote() 和noquote() 方法调整这些选项。此外,QTextStream manipulators 可以被管道传输到 QDebug 流中。

QDebugStateSaver 将格式化更改限制在当前作用域内。resetFormat() 将选项重置为默认值。

将自定义类型写入流

许多标准类型都可以写入 QDebug 对象,Qt 还支持大多数 Qt 值类型。要添加对自定义类型的支持,您需要实现一个流操作符,如下例所示:

QDebug operator<<(QDebug debug, const Coordinate &c)
{
    QDebugStateSaver saver(debug);
    debug.nospace() << '(' << c.x() << ", " << c.y() << ')';

    return debug;
}

相关内容详见《调试技术》和《创建自定义 Qt 类型》文档。

成员类型文档

enum QDebug::VerbosityLevel

此枚举描述了详细程度的范围。

常量值
QDebug::MinimumVerbosity0
QDebug::DefaultVerbosity2
QDebug::MaximumVerbosity7

另请参阅 verbosity() 和setVerbosity()。

成员函数文档

[explicit, since 6.9] QDebug::QDebug(QByteArray *byteArray)

构建一个调试流,该流将数据写入指定的byteArray 。

数据采用 UTF-8 编码,这可能与系统区域设置不一致,例如在 Windows 系统上。

对于使用此构造函数实例化的对象,数据可能会被缓冲,并且在流被刷新(例如,通过调用 `Qt::flush`)之前,不会写入字节数组。

该函数在 Qt 6.9 中引入。

另请参阅 QTextStream manipulators 。

[explicit] QDebug::QDebug(QIODevice *device)

构建一个调试流,该流将数据写入指定的device 。

[explicit] QDebug::QDebug(QString *string)

构建一个调试流,该流将数据写入指定的string 。

[explicit] QDebug::QDebug(QtMsgType t)

构建一个调试流,该流会将数据写入消息类型t 的处理程序中。

QDebug::QDebug(const QDebug &o)

创建另一个调试流o 的副本。

[noexcept] QDebug::~QDebug()

将所有待写数据刷新,并销毁调试流。

bool QDebug::autoInsertSpaces() const

如果该QDebug 实例会在写入操作之间自动插入空格,则返回true 。

另请参阅 setAutoInsertSpaces() 和QDebugStateSaver 。

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

根据当前引号自动插入设置,将字符c 写入调试流,并返回对该流的引用。

默认字符为双引号" 。

另请参见 quote() 和noquote()。

QDebug &QDebug::maybeSpace()

根据当前的自动插入空格设置,向调试流写入一个空格字符,并返回对该流的引用。

另请参阅 space() 和nospace()。

QDebug &QDebug::noquote()

禁用在QChar 、QString 和QByteArray 内容周围自动插入引号,并返回对该流的引用。

当禁用引号时,这些类型将被打印出来,且不带引号,也不会对不可打印字符进行转义。

另请参阅 quote() 和maybeQuote()。

QDebug &QDebug::nospace()

禁用空格的自动插入,并返回对该流的引用。

另请参阅 space() 和maybeSpace()。

QDebug &QDebug::quote()

启用对QChar 、QString 和QByteArray 内容自动添加引号的功能,并返回对该流的引用。

默认情况下启用加引号功能。

另请参阅 noquote() 和maybeQuote()。

[noexcept, since 6.7] bool QDebug::quoteStrings() const

如果该QDebug 实例会对流式输入的字符串进行引号封装(这是默认行为),则返回true 。

该函数在 Qt 6.7 中引入。

另请参阅 QDebugStateSaver 、quote()、noquote() 以及setQuoteStrings()。

QDebug &QDebug::resetFormat()

重置流的格式化选项,将其恢复到最初构建时的状态。

另请参阅 space() 和quote()。

void QDebug::setAutoInsertSpaces(bool b)

如果b 为真,则启用在写入操作之间自动插入空格的功能;否则,自动插入空格的功能将被禁用。

另请参阅 autoInsertSpaces() 和QDebugStateSaver 。

[since 6.7] void QDebug::setQuoteStrings(bool b)

如果b 的值为true ,则启用对流式传输到此QDebug 实例中的字符串的引号处理;否则,引号处理将被禁用。

默认情况下会对字符串进行加引号。

该函数在 Qt 6.7 中引入。

另请参阅 QDebugStateSaver 、quote()、noquote() 和quoteStrings()。

void QDebug::setVerbosity(int verbosityLevel)

将流的详细程度设置为verbosityLevel 。

允许的取值范围为 0 到 7。默认值为 2。

另请参阅 verbosity() 和VerbosityLevel 。

QDebug &QDebug::space()

将一个空格字符写入调试流,并返回该流的引用。

该流会记住已启用自动插入空格的功能,以便在今后的写入操作中使用。

另请参阅 nospace() 和maybeSpace()。

[noexcept] void QDebug::swap(QDebug &other)

将此调试流实例与other 互换。此操作速度极快,且绝不会失败。

[static, since 6.9] template <typename T> QByteArray QDebug::toBytes(const T &object)

这相当于将 `object ` 传递给 `QDebug::toString(object).toUtf8()`,但效率更高。

该函数在 Qt 6.9 中引入。

另请参阅 toString()。

[static, since 6.0] template <typename T> QString QDebug::toString(const T &object)

将object 流式输入到一个对字符串进行操作的QDebug 实例中,然后返回该字符串。

当您需要对象的文本表示形式用于调试,但无法使用 `operator<<` 时,此函数非常有用。例如:

QString str = QDebug::toString(list);

该字符串是通过nospace() 流式传输的。

该函数在 Qt 6.0 中引入。

另请参阅 ` toBytes()`。

int QDebug::verbosity() const

返回调试流的详细程度。

流操作符可以检查该值,以决定是否需要详细输出,并根据级别打印更多信息。数值越大,表示需要的信息越多。

允许的取值范围为 0 到 7。默认值为 2。

另请参阅 setVerbosity() 和VerbosityLevel 。

QDebug &QDebug::verbosity(int verbosityLevel)

将流的详细程度设置为verbosityLevel ,并返回该流的引用。

允许的范围为 0 到 7。默认值为 2。

另请参阅 verbosity()、setVerbosity() 和VerbosityLevel 。

[since 6.0] QDebug &QDebug::operator<<(QByteArrayView t)

将观察到的字节数组的数据(t )写入流,并返回该流的引用。

通常情况下,QDebug 会将数据以引号包围的形式打印出来,并将控制字符或非 US-ASCII 字符转换为相应的 C 转义序列(\xAB )。这样,输出始终符合 7 位标准,必要时可将字符串从输出中复制并粘贴回 C++ 源代码中。

若要不经转换地打印不可打印字符,请启用 `noquote()` 功能。请注意,某些QDebug 后端可能不支持 8 位兼容性。

有关示例,请参阅QByteArray 的重载。

该函数在 Qt 6.0 中引入。

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

将字符t 写入流,并返回该流的引用。 通常情况下,QDebug 会将控制字符和非US-ASCII字符以C语言转义序列或其Unicode值(\u1234 )的形式输出。若要不经转换地输出不可打印字符,请启用noquote()功能,但请注意,某些QDebug 后端可能不支持8位字符处理,因此可能无法表示t 。

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

将字符串t 写入流,并返回该流的引用。通常,QDebug 会将字符串连同引号一起打印出来,并将不可打印字符转换为相应的Unicode值(\u1234 )。

若要不进行转换地打印不可打印字符,请启用noquote() 功能。请注意,某些QDebug 后端可能不支持 8 位字符。

有关示例,请参阅QString 的重载。

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

将字符串视图s 写入流,并返回该流的引用。通常,QDebug 会将字符串用引号括起来打印,并将不可打印字符转换为其Unicode值(\u1234 )。

若要不进行转换地打印不可打印字符,请启用noquote() 功能。请注意,某些QDebug 后端可能不支持 8 位字符。

有关示例,请参阅QString 的重载版本。

[since 6.0] QDebug &QDebug::operator<<(QUtf8StringView s)

将字符串视图s 写入流,并返回该流的引用。

通常情况下,QDebug 会将引号内的数据打印出来,并将控制字符或非 US-ASCII 字符转换为相应的 C 转义序列(\xAB )。这样,输出始终符合 7 位标准,必要时可以将字符串从输出中复制并粘贴回 C++ 源代码中。

若要不经转换地打印不可打印字符,请启用 `noquote()` 功能。请注意,某些QDebug 后端可能不支持 8 位字符。

该函数在 Qt 6.0 中引入。

[since 6.7] template <typename T> requires if_qint128<T> QDebug &QDebug::operator<<(T i)

[since 6.7] template <typename T> requires if_quint128<T> QDebug &QDebug::operator<<(T i)

输出 128 位整数i 的字符串表示形式。

注意: 只有当 Qt 支持 128 位整数类型时,此运算符 才可用。如果您的构建环境中提供了 128 位整数类型,但 Qt 库在编译时未包含该类型,则该运算符会输出警告信息。

注意:由于 该运算符是一个函数模板,因此 不会对其参数进行隐式转换。其参数必须精确为 qint128 或 quint128。

这些函数在 Qt 6.7 中引入。

另请参阅 QT_SUPPORTS_INT128 。

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

将布尔值t 写入流,并返回该流的引用。

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

将字符t 写入流,并返回该流的引用。

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

将 UTF-16 字符t 写入流,并返回该流的引用。

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

将 UTF-32 字符t 写入流,并返回该流的引用。

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

将字节数组t 写入流,并返回该流的引用。 通常,QDebug 会将数组用引号括起来打印出来,并将控制字符或非US-ASCII字符转换为相应的C语言转义序列(\xAB )。这样,输出结果始终符合7位字符标准,必要时可以将字符串从输出中复制并粘贴回C++源代码中。

若要不进行转换直接打印不可打印字符,请启用noquote()功能。请注意,某些QDebug 后端可能不支持8位字符。

输出示例:

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";                 // 转义字符
qDebug() << ba;              // prints: "\x1B"

ba= "\xC3\xA1";
qDebug() << ba;              // prints: "\xC3\xA1"

ba=QByteArray("a\0b", 3);
qDebug() << ba;               // prints: "\a\x00""b"

请注意,QDebug 需要按照 C 和 C++ 语言拼接字符串字面量的方式,先关闭再重新打开字符串,这样字母“b”才不会被解释为前一个十六进制转义序列的一部分。

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

将字符串t 写入流,并返回该流的引用。通常,QDebug 会将字符串连同引号一起打印出来,并将不可打印字符转换为其Unicode值(\u1234 )。

若要不经转换直接打印不可打印字符,请启用noquote() 功能。请注意,某些QDebug 后端可能不支持 8 位字符。

输出示例:

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";                 // 转义字符
qDebug() << s;              // prints: "\u001B"

s= "\u00AD";               // 软连字符
qDebug() << s;              // prints: "\u00AD"

s= "\u00E1";               // 带 acute 符号的拉丁小写字母 A
qDebug() << s;              // prints: "á"

s= "a\u0301";              // “a”后跟组合性锐音符号
qDebug() << s;              // prints: "á";

s= "\u0430\u0301";         // 西里尔小写字母 A 后跟组合性 acute 音调符号
qDebug() << s;              // prints: "а́"

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

将以“\0 ”结尾的 UTF-8 字符串t 写入流,并返回该流的引用。输出时,该字符串绝不会被加引号或转义。 请注意,QDebug 在内部以 UTF-16 格式进行缓冲,为了使用某些后端,可能需要使用区域设置的编解码器将其转换为 8 位字符,这可能会导致输出乱码(mojibake)。建议仅使用 US-ASCII 字符串。

[since 6.0] QDebug &QDebug::operator<<(const char16_t *t)

将以 '\0' 结尾的 UTF-16 字符串t 写入流,并返回该流的引用。输出时,该字符串绝不会被加引号或转义。 请注意,QDebug 在内部以 UTF-16 格式缓冲数据,为了使用某些后端,可能需要使用区域设置的编解码器将其转换为 8 位字符,这可能会导致输出乱码(mojibake)。建议仅使用 US-ASCII 字符串。

该函数于 Qt 6.0 中引入。

[since 6.5] template <typename Char, typename... Args> QDebug &QDebug::operator<<(const std::basic_string<Char, Args...> &s)

[since 6.5] template <typename Char, typename... Args> QDebug &QDebug::operator<<(std::basic_string_view<Char, Args...> s)

将字符串或字符串视图s 写入流,并返回该流的引用。

只有当Char 属于以下情况之一时,这些运算符才会参与重载解析:

  • char
  • char8_t(仅限 C++20)
  • char16_t
  • char32_t
  • wchar_t

这些函数是在 Qt 6.5 中引入的。

[since 6.7] template <typename T> requires if_streamable<T> QDebug &QDebug::operator<<(const std::optional<T> &opt)

将opt (若未设置则为nullopt )中的内容写入此流。T 必须支持将数据流写入QDebug 。

该函数在 Qt 6.7 中引入。

[since 6.9] template <typename... Ts> requires if_streamable<Ts...> QDebug &QDebug::operator<<(const std::tuple<Ts...> &tuple)

将tuple 中的内容写入流。所有Ts... 都必须支持将数据流写入QDebug 。

该函数在 Qt 6.9 中引入。

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

将指针t 写入流中,并返回该流的引用。

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

将 64 位浮点数t 写入流,并返回该流的引用。

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

将 32 位浮点数t 写入流,并返回该流的引用。

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

将带符号整数t 写入流,并返回该流的引用。

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

将带符号的长整数t 写入流,并返回该流的引用。

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

将带符号的 64 位整数t 写入流,并返回该流的引用。

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

将该无符号 64 位整数(t )写入流,并返回该流的引用。

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

将带符号短整数t 写入流,并返回该流的引用。

[since 6.6] template <typename Rep, typename Period> QDebug &QDebug::operator<<(std::chrono::duration<Rep, Period> duration)

将时间长度duration 打印到流中,并返回该流的引用。打印出的字符串是该时间段的数值表示形式,后跟时间单位,类似于 C++ 标准库使用 `std::ostream` 时生成的结果。

该单位不进行本地化处理。

该函数于 Qt 6.6 中引入。

[since 6.7] QDebug &QDebug::operator<<(std::nullopt_t)

将 nullopt 写入流。

该函数自 Qt 6.7 起引入。

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

将该无符号整数t 写入流,并返回该流的引用。

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

将该无符号长整数(t )写入流,并返回该流的引用。

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

将该无符号短整数t 写入流中,并返回该流的引用。

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

将other 调试流分配给此流,并返回对此流的引用。

相关的非成员

template <typename Key, typename T> QDebug operator<<(QDebug debug, const QHash<Key, T> &hash)

将hash 中的内容写入debug 。Key 和T 都需要支持将数据流式传输到QDebug 。

template <typename T> QDebug operator<<(QDebug debug, const QList<T> &list)

将list 中的内容写入debug 。T 需要支持将数据流式传输到QDebug 。

template <typename Key, typename T> QDebug operator<<(QDebug debug, const QMap<Key, T> &map)

将map 中的内容写入debug 。Key 和T 都需要支持将数据流式传输到QDebug 。

template <typename Key, typename T> QDebug operator<<(QDebug debug, const QMultiHash<Key, T> &hash)

将hash 的内容写入debug 。Key 和T 都需要支持将数据流写入QDebug 。

template <typename Key, typename T> QDebug operator<<(QDebug debug, const QMultiMap<Key, T> &map)

将map 中的内容写入debug 。Key 和T 都必须支持将数据流式传输到QDebug 。

template <typename T> QDebug operator<<(QDebug debug, const QSet<T> &set)

将set 中的内容写入debug 。T 需要支持将数据流式传输到QDebug 。

[since 6.3] template <typename T, qsizetype P> QDebug operator<<(QDebug debug, const QVarLengthArray<T, P> &array)

将array 的内容写入debug 。T 需要支持将数据流写入QDebug 。

该函数在 Qt 6.3 中引入。

[since 6.9] template <typename T, std::size_t N> QDebug operator<<(QDebug debug, const std::array<T, N> &array)

将array 的内容写入debug 。T 需要支持将数据流写入QDebug 。

该函数在 Qt 6.9 中引入。

template <typename T, typename Alloc> QDebug operator<<(QDebug debug, const std::list<T, Alloc> &vec)

将列表vec 的内容写入debug 。T 需要支持将数据流写入QDebug 。

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

将map 中的内容写入debug 。Key 和T 都需要支持将数据流写入QDebug 。

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

将map 中的内容写入debug 。Key 和T 都需要支持将数据流式传输到QDebug 。

[since 6.9] template < typename Key, typename Compare, typename Alloc > QDebug operator<<(QDebug debug, const std::multiset<Key, Compare, Alloc> &multiset)

将multiset 的内容写入debug 。Key 类型需要支持将数据流写入QDebug 。

该函数在 Qt 6.9 中引入。

template <typename T1, typename T2> QDebug operator<<(QDebug debug, const std::pair<T1, T2> &pair)

将pair 中的内容写入debug 。T1 和T2 都需要支持将数据流写入QDebug 。

[since 6.9] template < typename Key, typename Compare, typename Alloc > QDebug operator<<(QDebug debug, const std::set<Key, Compare, Alloc> &set)

将set 的内容写入debug 。Key 类型需要支持将数据流写入QDebug 。

该函数在 Qt 6.9 中引入。

[since 6.9] template < typename Key, typename T, typename Hash, typename KeyEqual, typename Alloc > QDebug operator<<(QDebug debug, const std::unordered_map<Key, T, Hash, KeyEqual, Alloc> &map)

将map 中的内容写入debug 。Key 和T 都需要支持将数据流写入QDebug 。

该函数在 Qt 6.9 中引入。

[since 6.9] template < typename Key, typename Hash, typename KeyEqual, typename Alloc > QDebug operator<<(QDebug debug, const std::unordered_set<Key, Hash, KeyEqual, Alloc> &unordered_set)

将unordered_set 的内容写入debug 。Key 类型需要支持流式写入QDebug 。

该函数于 Qt 6.9 中引入。

template <typename T, typename Alloc> QDebug operator<<(QDebug debug, const std::vector<T, Alloc> &vec)

将向量vec 的内容写入debug 。T 需要支持将数据流写入QDebug 。

[since 6.7] QDebug operator<<(QDebug d, QAnyStringView s)

将s 输出到调试流d 。

如果 `d.quotedString() ` 为 `true`,则表示字符串采用的编码格式。若仅需字符串数据,请按如下方式使用 `visit()`:

s.visit([&d) (auto s) { d << s; });

该函数在 Qt 6.7 中引入。

另请参阅 QAnyStringView::visit()。

[since 6.9] template <typename T> requires if_ordering_type<T> QDebug operator<<(QDebug debug, T t)

将 Qt 或 std 排序值t 写入debug 对象。

约束

仅当T 属于 <QT/Std>::<WEAK/partial/strong>_ordering 之一时,才会参与重载解析。

该函数于 Qt 6.9 中引入。

template <typename T> QDebug operator<<(QDebug debug, const QContiguousCache<T> &cache)

将cache 中的内容写入debug 。T 需要支持将数据流写入QDebug 。

template <typename T> QDebug operator<<(QDebug debug, const QFlags<T> &flags)

将flags 写入debug 。

宏文档

QDebug qCritical()

返回一个QDebug 对象,该对象会将一条严重级别的消息记录到中央消息处理程序中。

示例:

qCritical() << "Brush:" << myQBrush << "Other value:" << i;

使用 qCritical() 是qCritical(const char *, ...) 的另一种实现方式,后者遵循 printf 模式。

请注意,QDebug 以及类型特定的流运算符会添加各种格式化处理,以使调试消息更易于阅读。更多详细信息请参阅formatting options 文档。

出于调试目的,有时让程序在遇到关键消息时中止会比较方便。这样您就可以检查核心转储,或附加调试器——另请参阅qFatal()。要启用此功能,请将环境变量QT_FATAL_CRITICALS 设置为一个数字n 。此时,程序将在第 n 个关键消息出现时终止。 也就是说,如果该环境变量被设置为 1,程序将在首次调用时终止;如果其值为 10,则将在第 10 次调用时退出。环境变量中的任何非数值均等同于 1。

注意:此宏是线程安全的。

另请参阅 qCritical(const char *, ...) 和qCCritical()。

QDebug qDebug()

返回一个QDebug 对象,该对象会将一条调试消息记录到中央消息处理程序中。

示例:

qDebug() << "Brush:" << myQBrush << "Other value:" << i;

使用 qDebug() 是qDebug(const char *, ...) 的替代方案,后者遵循 printf 模式。

请注意,QDebug 以及类型特定的流运算符会添加各种格式化处理,以便于阅读调试信息。更多详情请参阅formatting options 文档。

如果编译时定义了QT_NO_DEBUG_OUTPUT ,则该函数不执行任何操作。

注意:此宏是线程安全的。

另请参阅 qDebug(const char *, ...) 和qCDebug()。

QDebug qFatal()

返回一个QDebug 对象,该对象会将一条致命错误消息记录到中央消息处理程序中。

使用 qFatal() 是qFatal(const char *, ...) 的替代方案,后者遵循 printf 模式。

请注意,QDebug 和特定类型的流运算符会添加各种格式化处理,以使调试消息更易于阅读。有关更多详细信息,请参阅formatting options 文档。

如果您使用的是默认消息处理程序,返回的流将终止以生成核心转储。在 Windows 上,对于调试构建,此函数将报告一个 _CRT_ERROR,从而允许您将调试器连接到应用程序。

注意:此宏是线程安全的。

另请参阅 qFatal(const char *, ...) 和qCFatal()。

QDebug qInfo()

返回一个QDebug 对象,该对象会向中央消息处理程序记录一条信息级消息。

示例:

qInfo() << "Brush:" << myQBrush << "Other value:" << i;

使用 qInfo() 是qInfo(const char *, ...) 的替代方案,后者遵循 printf 模式。

请注意,QDebug 以及类型特定的流运算符会添加各种格式化处理,以使调试信息更易于阅读。更多详细信息请参阅formatting options 文档。

如果编译时定义了QT_NO_INFO_OUTPUT ,则该函数不执行任何操作。

注意:此宏是线程安全的。

另请参阅 qInfo(const char *, ...) 和qCInfo()。

QDebug qWarning()

返回一个QDebug 对象,该对象会向中央消息处理程序记录一条警告消息。

示例:

qWarning() << "Brush:" << myQBrush << "Other value:" << i;

使用 qWarning() 是qWarning(const char *, ...) 的替代方案,后者遵循 printf 模式。

请注意,QDebug 以及特定类型的流运算符会添加各种格式化处理,以使调试信息更易于阅读。更多详细信息请参阅formatting options 文档。

如果编译时定义了QT_NO_WARNING_OUTPUT ,则该函数不执行任何操作。

出于调试目的,有时让程序在出现警告消息时终止会比较方便。这样你就可以检查核心转储,或附加调试器——另请参阅qFatal()。要启用此功能,请将环境变量QT_FATAL_WARNINGS 设置为一个数字n 。此时,程序将在第 n 个警告出现时终止。 也就是说,如果环境变量设置为 1,程序将在第一次调用时终止;如果设置为 10,则在第 10 次调用时退出。环境变量中的任何非数值均视为 1。

注意:此宏是线程安全的。

另请参阅 qWarning(const char *, ...) 和qCWarning()。

© 2026 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.