为什么?
这是因为qDebug()
.
从源代码:
inline QDebug &operator<<(QChar t) { stream->ts << '\'' << t << '\''; return maybeSpace(); }
inline QDebug &operator<<(const char* t) { stream->ts << QString::fromAscii(t); return maybeSpace(); }
inline QDebug &operator<<(const QString & t) { stream->ts << '\"' << t << '\"'; return maybeSpace(); }
所以,
QChar a = 'H';
char b = 'H';
QString c = "Hello";
qDebug()<<a;
qDebug()<<b;
qDebug()<<c;
输出
'H'
H
"Hello"
评论
那么为什么 Qt 这样做呢?由于qDebug
是出于调试的目的,各种类型的输入都会变成文本流输出qDebug
。
例如,qDebug
将布尔值打印到文本表达式true
/中false
:
inline QDebug &operator<<(bool t) { stream->ts << (t ? "true" : "false"); return maybeSpace(); }
它输出true
或输出false
到您的终端。因此,如果您有QString
which store true
,则需要一个引号"
来指定类型。