QDebug小知识

QDebug在开发过程中使用得较多,整理了一些较少用却很有用的知识。

禁用自动插入空格

1
QDebug &QDebug::nospace()

对比:

1
2
3
4
5
qDebug() << "Hello" << "world!";
qDebug().nospace() << "Hello" << "world!";
输出:
Hello world!
Helloworld!

禁用引号字符

  禁用在 QChar,QString 和 QByteArray内容周围自动插入引号字符。当开启引号字符禁用时,这些类型的打印将不带引号字符,也不会转义不可打印的字符。

1
QDebug &QDebug::noquote()

对比:

1
2
3
4
5
qDebug() << QString("Hello world!");  
qDebug().noquote() << QString("Hello world!");
输出:
"Hello world!"
Hello world!

不需要引用QDebug头文件也可使用qDebug()

  如果向函数传递格式字符串和参数列表,则其工作方式与C语言的printf()函数类似。格式应为Latin-1字符串。

1
qDebug(const char *message, ...)

如:

1
qDebug("%s", "Hello world!");

屏蔽qDebug打印

  项目文件(.pro)添加

1
DEFINES+= QT_NO_DEBUG_OUTPUT