如何在不添加空格和换行符的情况下调用 qDebug?

发布于 2024-10-20 16:08:41 字数 687 浏览 8 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

家住魔仙堡 2024-10-27 16:08:41

最好了解 QDebug 在内部工作。这样您就可以轻松修改它以满足您的需求。每当您使用 qDebug() 函数时,它都会返回一个 QDebug 对象。默认情况下,QDebug 始终在使用运算符 << 后输出一个空格。

QDebug 类内部包含一个QString。每次使用运算符<<时,您都会附加到该内部QString。当QDebug对象被销毁时,这个QString通过qt_message_output(QtMsgType, char*)打印。

默认情况下,qt_message_output 始终打印后跟换行符的字符串。

正常输出

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

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

没有空格

您可以使用QDebug::nospace来告诉QDebug不要在每次调用操作符<<之后添加空格。

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

这将输出 Var1,因为 QDebug 对象不再打印空格。

没有换行

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

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

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

如果您不想打印新行,则必须更改 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";

这将打印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 the qDebug() function, it returns a QDebug object. By default QDebug always outputs a space after any use of operator <<.

The QDebug class 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 QDebug object is destroyed.

By default qt_message_output always prints the string followed by a newline.

Normal Output

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

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

Without Spaces

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

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

This will output Var1 as that QDebug object is no longer printing spaces.

Without New Lines

Not adding the \n at the end of the string is a little bit harder. Since QDebug internally only passes the string to qt_message_output when it is destroyed, you can delay the destruction of that QDebug object -

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

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.

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.

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 of printf. This way you can avoid the extra spaces that are appended by QDebug.

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

享受孤独 2024-10-27 16:08:41

尝试以下格式: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 formatting

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

萌面超妹 2024-10-27 16:08:41

从 Qt 5.4 开始你还可以这样写:

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

Since Qt 5.4 you can also write:

qDebug().nospace().noquote() << var1;
懷念過去 2024-10-27 16:08:41

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

qDebug() << qPrintable(var1);

消除周围的引号。

Combining some of the above answers you can use

qDebug() << qPrintable(var1);

to eliminate the surrounding quotes.

箹锭⒈辈孓 2024-10-27 16:08:41

我也遇到了报价问题。解决方案是不将 QString() 通过管道传输到流中,而是使用 QString(...).toStdString().c_str()

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

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

现在每次您使用 QString 时,都可以这样做:

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

I also experienced the quotes problem. The solution is to not pipe QString() into the stream but instead 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:

qDebug() << Q(var1) << "=" << var2;
秉烛思 2024-10-27 16:08:41

文件 $(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.

枫林﹌晚霞¤ 2024-10-27 16:08:41

实例化一个 QDebug 对象并向其输出:

QDebug dbg = qDebug().nospace().noquote();

dbg << var1 << "=" << var2;

产量:

some string=1

按照您想要的方式输出到 dbg 对象 - 在超出范围之前不会出现换行符。例如:

char var1[] = "some string";
int var2 = 1;

{
    QDebug dbg = qDebug().nospace().noquote();

    dbg << var1 << "=" << var2;

    // keep using "dbg"; there's no newline ('\n') until it destructs
    dbg << "...";
    for (int i = 5; i <=9; ++i)
        dbg << i;
}

输出:

some string=1...56789

Instantiate a QDebug object and output to it:

QDebug dbg = qDebug().nospace().noquote();

dbg << var1 << "=" << var2;

Yields:

some string=1

Output to the dbg object all you want -- there won't be a newline until it goes out of scope. For example:

char var1[] = "some string";
int var2 = 1;

{
    QDebug dbg = qDebug().nospace().noquote();

    dbg << var1 << "=" << var2;

    // keep using "dbg"; there's no newline ('\n') until it destructs
    dbg << "...";
    for (int i = 5; i <=9; ++i)
        dbg << i;
}

Outputs:

some string=1...56789
沒落の蓅哖 2024-10-27 16:08:41

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文