qDebug()输出QString,转义字符不生效

现象:当我在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后生效了。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值