How to send a QString to debug output?

user929404 picture user929404 · Jan 20, 2014 · Viewed 9.6k times · Source

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

Answer

SirDarius picture SirDarius · Jan 20, 2014

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.