C++ 如何在没有附加空格和换行符的情况下调用 qDebug?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5209823/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:40:27  来源:igfitidea点击:

How to call qDebug without the appended spaces and newline?

c++qtqdebug

提问by Johan

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.

我正在使用 C++/Qt 打印函数 qDebug,但有时我想控制“、空格和换行符的附加方式,而不是使用默认的 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?

然后我如何调用 qDebug?



Note: There is a function in qDebug called nospace, but it will remove the spaces. But the " is still there.

注意:qDebug 中有一个名为 nospace的函数,但它会删除空格。但是“仍然存在。

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

/谢谢

采纳答案by Johnny

Try this format: qDebug("%s=%d", "string", 1);In this case qDebuguses printfformatting

试试这种格式:qDebug("%s=%d", "string", 1);在这种情况下qDebug使用printf格式

P.S. Adapted for your example: qDebug("%s=%d", var1.toStdString().c_str(), var2);

PS 适用于您的示例: qDebug("%s=%d", var1.toStdString().c_str(), var2);

回答by Vishesh Handa

It would be best to understand how QDebugworks internally. That way you can easily modify it to suit your needs. Whenever you use the qDebug()function, it returns a QDebugobject. By default QDebugalways outputs a space after any use of operator <<.

最好了解QDebug内部的工作原理。这样您就可以轻松修改它以满足您的需要。无论何时使用该qDebug()函数,它都会返回一个QDebug对象。默认情况下,QDebug在任何使用operator <<.

The QDebugclass internally contains a QString. Every time you use operator <<you are appending to that internal QString. This QString is printed via qt_message_output(QtMsgType, char*)when the QDebugobject is destroyed.

QDebug类内部含有QString。每次使用时,operator <<都会附加到该内部 QString。qt_message_output(QtMsgType, char*)QDebug对象被销毁时,这个 QString 被打印出来。

By default qt_message_outputalways prints the string followed by a newline.

默认情况下qt_message_output总是打印后跟换行符的字符串。

Normal Output

正常输出

qDebug() << "Var" << 1;

This will output Var 1. This is because qDebugwill create a QDebugobject which appends a space after each call to operator <<. So that will be Var+ + 1 + .

这将输出Var 1. 这是因为qDebug将创建一个QDebug对象,该对象在每次调用operator <<. 所以这将是Var+ + 1 +

Without Spaces

没有空格

You can use QDebug::nospaceto tell QDebugnot to append a space after each call to operator <<.

您可以使用QDebug::nospace来告诉QDebug不要在每次调用后附加空格operator <<

qDebug().nospace() << "Var" << 1;

This will output Var1as that QDebugobject is no longer printing spaces.

这将输出Var1为该QDebug对象不再打印空间。

Without New Lines

没有换行

Not adding the \nat the end of the string is a little bit harder. Since QDebuginternally only passes the string to qt_message_outputwhen it is destroyed, you can delay the destruction of that QDebug object -

不在\n字符串的末尾添加有点困难。由于QDebug内部仅在qt_message_output销毁字符串时将其传递给,因此您可以延迟该 QDebug 对象的销毁 -

QDebug deb = qDebug();
deb << "One" << "Two";
deb << "Three";

This will print One Two Threeand then append a new line.

这将打印One Two Three,然后追加一个新行。

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.

如果您永远不想打印新行,则必须更改qt_message_output. 这可以通过安装自定义处理程序来完成。

void customHandler(QtMsgType type, const char* msg) {
    fprintf(stderr, msg);
    fflush(stderr);
}

// Somewhere in your program
qInstallMsgHandler(customHandler);

qDebug() << "One" << "Two";
qDebug().noSpace() << "Three" << "Four";

This will print One Two ThreeFour.

这将打印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 语句。如果要删除自定义处理程序,则应调用qInstallMsgHandler(0).

qDebug(const char* msg, ...)

qDebug(const char* msg, ...)

As indicated by the other answers you can also use the qDebugfunction to print strings in a format similar to that of printf. This way you can avoid the extra spaces that are appended by QDebug.

如其他答案所示,您还可以使用该qDebug函数以类似于printf. 这样您就可以避免附加的额外空格QDebug

However, qDebuginternally still uses qt_message_output, so you will still get a newline at the end unless you install your own handler.

但是,qDebug内部仍然使用qt_message_output,因此除非您安装自己的处理程序,否则最后您仍然会得到一个换行符。

回答by user4444617

Since Qt 5.4 you can also write:

从 Qt 5.4 开始,您还可以编写:

qDebug().nospace().noquote() << var1;

回答by Tim MB

Combining some of the above answers you can use

结合上面的一些答案,您可以使用

qDebug() << qPrintable(var1);

to eliminate the surrounding quotes.

消除周围的引号。

回答by hurikhan77

I also experienced the quotes problem. The solution is to not pipe QString()into the stream but instead QString(...).toStdString().c_str().

我也遇到了报价问题。解决方案是不通过管道QString()进入流,而是QString(...).toStdString().c_str().

I've built myself a small convenience macro to easily get around this:

我为自己建立了一个方便的小宏来轻松解决这个问题:

#define Q(string) (string).toStdString().c_str()

Now everytime you use a QString, do it like that:

现在每次使用 QString 时,请这样做:

qDebug() << Q(var1) << "=" << var2;

回答by hmuelner

The file $(QTDIR)/src/corelib/io/qdebug.h contains almost all definitions for the debug output methods. One of them is:

文件 $(QTDIR)/src/corelib/io/qdebug.h 包含调试输出方法的几乎所有定义。其中之一是:

inline QDebug &operator<<(const QString & t) { stream->ts << '\"' << t << '\"'; return maybeSpace(); }

内联 QDebug &operator<<(const QString & t) { stream->ts << '\"' << t << '\"'; 返回也许空间();}

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.h 或使用你自己的副本或 QDebug 类的修改和重命名的副本。

回答by zkunov

Another way is to use your own message handler.
Hope this helps.

另一种方法是使用您自己的消息处理程序
希望这可以帮助。