QDebug小知識

  • 2019 年 11 月 12 日
  • 筆記

QDebug在開發過程中使用得較多,整理了一些較少用卻很有用的知識。

禁用自動插入空格

QDebug &QDebug::nospace()

對比:

qDebug() << "Hello" << "world!";  qDebug().nospace() << "Hello" << "world!";  輸出:  Hello world!  Helloworld!

禁用引號字元

  禁用在 QChar,QString 和 QByteArray內容周圍自動插入引號字元。當開啟引號字元禁用時,這些類型的列印將不帶引號字元,也不會轉義不可列印的字元。

QDebug &QDebug::noquote()

對比:

qDebug() << QString("Hello world!");  qDebug().noquote() << QString("Hello world!");  輸出:  "Hello world!"  Hello world!

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

  如果向函數傳遞格式字元串和參數列表,則其工作方式與C語言的printf()函數類似。格式應為Latin-1字元串。

qDebug(const char *message, ...)

如:

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

屏蔽qDebug列印

  項目文件(.pro)添加

DEFINES+= QT_NO_DEBUG_OUTPUT