How to print string literal and QString with qDebug?
Asked Answered
T

6

34

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?

QString s = "value";
qDebug("abc" + s + "def");
Tallia answered 25/8, 2013 at 8:44 Comment(0)
Z
22

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");
Zonazonal answered 25/8, 2013 at 8:50 Comment(0)
G
25

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()).

Gastrointestinal answered 25/8, 2013 at 10:7 Comment(1)
It's recommended to use qUtf8Printable over qPrintable according to the docs, because qDebug() (and friends) expect UTF-8 which may not always be what toLocal8Bits() (which is what qPrintable calls) returnsTortfeasor
Z
22

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");
Zonazonal answered 25/8, 2013 at 8:50 Comment(0)
T
19

According to Qt Core 5.6 documentation you should use qUtf8Printable() from <QtGlobal> header to print QString with qDebug.

You should do as follows:

QString s = "some text";
qDebug("%s", qUtf8Printable(s));

or shorter:

QString s = "some text";
qDebug(qUtf8Printable(s));

See:

Timber answered 23/4, 2016 at 22:21 Comment(1)
this should really be the new accepted answer. I would definitely prefer to use qUtf8Printable and qPrintable over .toLatin1().constData() and C++ << operator.Professor
T
8

Option 1: Use qDebug's default mode of a C-string format and variable argument list (like printf):

qDebug("abc%sdef", s.toLatin1().constData());

Option 2: Use the C++ version with overloaded << operator:

#include <QtDebug>
qDebug().nospace() << "abc" << qPrintable(s) << "def";

Reference: https://qt-project.org/doc/qt-5-snapshot/qtglobal.html#qDebug

Teetotalism answered 15/12, 2014 at 7:19 Comment(0)
A
4

Just rewrite your code like this:

QString s = "value";
qDebug() << "abc" << s << "def";
Anesthesiologist answered 25/8, 2013 at 8:57 Comment(1)
That's not the same. Your code returns 'abc "value" def'. His code 'abcvaluedef'. Different use case.Zonazonal
S
1

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:

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 .

Stencil answered 22/6, 2017 at 7:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.