Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send a QString to debug output? [duplicate]

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

like image 559
user929404 Avatar asked Jan 20 '14 11:01

user929404


People also ask

How do you convert QString to double?

How do you convert QString to double in Qt? lineEdit->setText("123456789"); QVariant val = lineEdit->text(). toDouble();

How do you cast QString to string?

You can use: QString qs; // do things std::cout << qs. toStdString() << std::endl; It internally uses QString::toUtf8() function to create std::string, so it's Unicode safe as well.

What is QDebug function?

QDebug is used whenever the developer needs to write out debugging or tracing information to a device, file, string or console.


1 Answers

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.

like image 185
SirDarius Avatar answered Sep 21 '22 00:09

SirDarius