How to send a QString to debug output? [duplicate]
Asked Answered
M

1

6

I cannot print out a QString in Qt using QDebug.

Below are some attempts (none work):

    QDebug(letters.toStdString());
    QDebug(letters.toLatin1());
    QDebug() << letters.toUtf8();
    QDebug() << letters.toWCharArray();
    QDebug() << letters.toStdString();
    QDebug() << letters;

I have included:

#include <QtDebug>
#include <QDebug>

I am using Qt 5.2. I also added CONFIG += console to my project file

My error is "no matching function for call to QDebug::QDebug()"

I also got "QDebug(QByteArray) is ambiguous" for QDebug(letters.toLatin1());

Maund answered 20/1, 2014 at 11:34 Comment(7)
What error do you get ?Fastening
you should use it a little different way: qDebug() << letters;Poler
@Fastening I edited the question to answer you.Maund
@Poler Look at the sixth attemptMaund
QDebug and qDebug are not the same...Fastening
@Maund Look at the lower-case letter q in my commentPoler
That was pretty dumb of me. Sorry guysMaund
F
13

The correct way to do so is:

#include <QDebug>

// snip...

QString letters;

qDebug() << letters;

Be careful to use qDebug() beginning with a small letter, since it is not the same thing as the QDebug class.

See http://qt-project.org/doc/qt-5.0/qtcore/qtglobal.html#qDebug. It is a convenience function that returns an already configured QDebug object.

Fastening answered 20/1, 2014 at 11:41 Comment(3)
That is what I have done and it doesn't workMaund
@Maund it is qDebug and not QDebug. Read. Don't guess.Bagpipes
worth to mention that you can print QString with qDebug("%s", qPrintable(qstring_instance));Alternative

© 2022 - 2024 — McMap. All rights reserved.