如何在不添加空格和换行符的情况下调用 qDebug?
我正在使用C++/Qt打印函数qDebug, 但有时我想控制如何附加“、空格和换行符 并且不使用默认的 qDebug。
让我们举一个简单的例子:
QString var1("some string");
int var2 = 1;
qDebug() << var1 << "=" << var2;
这将打印
"some string" = 1
但是假设我不喜欢附加的“和空格 并希望打印看起来像
some string=1
如何然后调用 qDebug?
注意:qDebug 中有一个名为 nospace 的函数,但它会删除空格。 但“仍然存在。
如果我使用这个:
qDebug().nospace() << var1 << "=" << var2;
我得到:
"some string"=1
但请注意,我仍然没有找到摆脱结尾换行符的方法。/
谢谢
I'm using the the C++/Qt print function qDebug,
but sometimes I would like to control how ", space and newline is appended
and not use the default qDebug.
Let's take a simple example:
QString var1("some string");
int var2 = 1;
qDebug() << var1 << "=" << var2;
This will print
"some string" = 1
But Let's say that I don't like the appended " and space
and would like the print to look like
some string=1
How to I then call qDebug?
Note: There is a function in qDebug called nospace, but it will remove the spaces.
But the " is still there.
If I use this:
qDebug().nospace() << var1 << "=" << var2;
I get:
"some string"=1
But please note that I have still not found a way to get rid of the ending newline.
/Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
最好了解
QDebug
在内部工作。这样您就可以轻松修改它以满足您的需求。每当您使用qDebug()
函数时,它都会返回一个QDebug
对象。默认情况下,QDebug
始终在使用运算符 <<
后输出一个空格。QDebug
类内部包含一个QString
。每次使用运算符<<
时,您都会附加到该内部QString。当QDebug
对象被销毁时,这个QString通过qt_message_output(QtMsgType, char*)
打印。默认情况下,
qt_message_output
始终打印后跟换行符的字符串。正常输出
这将输出
Var 1
。这是因为qDebug
将创建一个QDebug
对象,该对象在每次调用operator <<
后附加一个空格。所以这将是Var
++ 1 +
。
没有空格
您可以使用
QDebug::nospace
来告诉QDebug
不要在每次调用操作符<<
之后添加空格。这将输出
Var1
,因为QDebug
对象不再打印空格。没有换行
在字符串末尾不添加
\n
有点困难。由于QDebug
内部仅在销毁时将字符串传递给qt_message_output
,因此您可以延迟该QDebug对象的销毁 -这将打印
One Two Three
> 然后追加一个新行。如果您不想打印新行,则必须更改
qt_message_output
的行为。这可以通过安装自定义处理程序来完成。这将打印
One Two ThreeFour
。请注意,这将影响程序中的所有 qDebug 语句。如果您想删除自定义处理程序,您应该调用
qInstallMsgHandler(0)
。qDebug(const char* msg, ...)
正如其他答案所示,您还可以使用
qDebug
函数以类似于printf
。这样您就可以避免 QDebug 附加的额外空格。但是,
qDebug
内部仍然存在使用 qt_message_output ,因此除非您安装自己的处理程序,否则您仍然会在末尾得到换行符。It would be best to understand how
QDebug
works internally. That way you can easily modify it to suit your needs. Whenever you use theqDebug()
function, it returns aQDebug
object. By defaultQDebug
always outputs a space after any use ofoperator <<
.The
QDebug
class internally contains aQString
. Every time you useoperator <<
you are appending to that internal QString. This QString is printed viaqt_message_output(QtMsgType, char*)
when theQDebug
object is destroyed.By default
qt_message_output
always prints the string followed by a newline.Normal Output
This will output
Var 1
. This is becauseqDebug
will create aQDebug
object which appends a space after each call tooperator <<
. So that will beVar
++ 1 +
.
Without Spaces
You can use
QDebug::nospace
to tellQDebug
not to append a space after each call tooperator <<
.This will output
Var1
as thatQDebug
object is no longer printing spaces.Without New Lines
Not adding the
\n
at the end of the string is a little bit harder. SinceQDebug
internally only passes the string toqt_message_output
when it is destroyed, you can delay the destruction of that QDebug object -This will print
One Two Three
and then append a new line.If you never want a new line to be printed, you will have to change the behaviour of
qt_message_output
. This can be done by installing a custom handler.This will print
One Two ThreeFour
.Be warned that this will affect all of the qDebug statements in your program. If you want to remove the custom handler, you should call
qInstallMsgHandler(0)
.qDebug(const char* msg, ...)
As indicated by the other answers you can also use the
qDebug
function to print strings in a format similar to that ofprintf
. This way you can avoid the extra spaces that are appended byQDebug
.However,
qDebug
internally still usesqt_message_output
, so you will still get a newline at the end unless you install your own handler.尝试以下格式:
qDebug("%s=%d", "string", 1);
在这种情况下,
qDebug
使用 printf格式适合您的示例:
qDebug("%s=%d", var1.toStdString().c_str(), var2);
Try this format:
qDebug("%s=%d", "string", 1);
In this case
qDebug
uses printf formattingP.S. Adapted for your example:
qDebug("%s=%d", var1.toStdString().c_str(), var2);
从 Qt 5.4 开始你还可以这样写:
Since Qt 5.4 you can also write:
结合上面的一些答案,您可以用来
消除周围的引号。
Combining some of the above answers you can use
to eliminate the surrounding quotes.
我也遇到了报价问题。解决方案是不将
QString()
通过管道传输到流中,而是使用QString(...).toStdString().c_str()
。我为自己构建了一个方便的小宏来轻松解决这个问题:
现在每次您使用 QString 时,都可以这样做:
I also experienced the quotes problem. The solution is to not pipe
QString()
into the stream but insteadQString(...).toStdString().c_str()
.I've built myself a small convenience macro to easily get around this:
Now everytime you use a QString, do it like that:
文件 $(QTDIR)/src/corelib/io/qdebug.h 包含调试输出方法的几乎所有定义。其中之一是:
内联 QDebug &operator<<(const QString & t) { Stream->ts << '\"' << t << '\"';返回 MaybeSpace(); 因此,
没有“官方”方法来抑制引号,但您当然可以更改 qdebug.h 或使用您自己的副本或 QDebug 类的修改和重命名副本。
The file $(QTDIR)/src/corelib/io/qdebug.h contains almost all definitions for the debug output methods. One of them is:
inline QDebug &operator<<(const QString & t) { stream->ts << '\"' << t << '\"'; return maybeSpace(); }
So there is no "official" way to suppress the quotes, but you can of course change qdebug.h or use your own copy or a modified and renamed copy of the QDebug class.
实例化一个 QDebug 对象并向其输出:
产量:
按照您想要的方式输出到 dbg 对象 - 在超出范围之前不会出现换行符。例如:
输出:
Instantiate a
QDebug
object and output to it:Yields:
Output to the
dbg
object all you want -- there won't be a newline until it goes out of scope. For example:Outputs:
另一种方法是使用您的自己的消息处理程序。< br>
希望这有帮助。
Another way is to use your own message handler.
Hope this helps.