C++ 如何使用 qDebug 打印字符串文字和 QString?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18427191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print string literal and QString with qDebug?
提问by Meysam
Is there any easy way to get the following work? I mean is there any helper class in Qt
which prepares the string for qDebug
?
有没有什么简单的方法可以完成以下工作?我的意思是是否有任何帮助类Qt
为 准备字符串qDebug
?
QString s = "value";
qDebug("abc" + s + "def");
采纳答案by Greenflow
No really easy way I am aware of. You can do:
我知道没有真正简单的方法。你可以做:
QByteArray s = "value";
qDebug("abc" + s + "def");
or
或者
QString s = "value";
qDebug("abc" + s.toLatin1() + "def");
回答by Kurt Pattyn
You can use the following:
您可以使用以下内容:
qDebug().nospace() << "abc" << qPrintable(s) << "def";
The nospace()
is to avoid printing out spaces after every argument (which is default for qDebug()
).
这nospace()
是为了避免在每个参数之后打印出空格(这是 的默认值qDebug()
)。
回答by Krzysiek
According to Qt Core 5.6 documentationyou should use qUtf8Printable()
from <QtGlobal>
header to print QString
with qDebug
.
据Qt的核心5.6文档,你应该使用qUtf8Printable()
的<QtGlobal>
头打印QString
用qDebug
。
You should do as follows:
你应该这样做:
QString s = "some text";
qDebug("%s", qUtf8Printable(s));
or shorter:
或更短:
QString s = "some text";
qDebug(qUtf8Printable(s));
See:
看:
回答by bleater
Option 1: Use qDebug's default mode of a C-string format and variable argument list (like printf):
选项 1:使用 qDebug 的 C 字符串格式和变量参数列表(如 printf)的默认模式:
qDebug("abc%sdef", s.toLatin1().constData());
Option 2: Use the C++ version with overloaded << operator:
选项 2:使用带有重载 << 运算符的 C++ 版本:
#include <QtDebug>
qDebug().nospace() << "abc" << qPrintable(s) << "def";
Reference: https://qt-project.org/doc/qt-5-snapshot/qtglobal.html#qDebug
参考:https: //qt-project.org/doc/qt-5-snapshot/qtglobal.html#qDebug
回答by troyane
Just rewrite your code like this:
只需像这样重写您的代码:
QString s = "value";
qDebug() << "abc" << s << "def";
回答by JAV
I know this question is a bit old, but it appears nearly on top when searching for it in the web. One can overload the operator for qDebug (more specific for QDebug) to make it accept std::strings like this:
我知道这个问题有点老,但在网上搜索时它几乎出现在最前面。可以重载 qDebug 的运算符(更具体为 QDebug)以使其接受 std::strings ,如下所示:
inline QDebug operator<<(QDebug dbg, const std::string& str)
{
dbg.nospace() << QString::fromStdString(str);
return dbg.space();
}
This thing is for years in all of my projects, I nearly forget it is still not there by default.
这个东西在我所有的项目中已经存在多年了,我几乎忘记它默认情况下仍然不存在。
After that, usage of << for qDebug() is a lot more usable imho. You can even mix QString and std::string. Some additional(but not really intended) feature is, that you sometimes can throw in integers or other types that allow implicit conversion to std::string .
在那之后,对于 qDebug() 使用 << 是更有用的恕我直言。你甚至可以混合使用 QString 和 std::string。一些额外的(但不是真正打算的)功能是,您有时可以抛出整数或其他类型,这些类型允许隐式转换为 std::string 。