现象:当我在QString中加入制表符\t,并用qDebug()打印的时候,发现并不起作用。
QString str = "\t\thello world\nQT";
qDebug()<<str;
输出:
"\t\thello world\nQT"
文档中 QDebug &QDebug::operator<<(const QString &t) 的描述:
Writes the string, t, 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.
翻译:将字符串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"; // 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().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.
翻译
禁用在 QChar、QString 和 QByteArray 内容周围自动插入引号字符,并返回对流的引用。
禁用引号时,打印这些类型时不带引号字符,也不转义不可打印字符。
修改后:
QString str = "\t\thello world\nQT";
qDebug()<<str;
qDebug().noquote()<<str;
输出:
"\t\thello world\nQT"
hello world
QT
可以看到加了noquote后生效了。