c++qtstdoutqdebugqtextstream

In Qt: Can I output to `stdout`, as easy as I can output to `stderr` using qDebug()?


Up until now, I output everything using qDebug().noquote(). This is easy because it just requires a simple #import <QDebug>

Now I need everything to output to stdout, but I don't know how to do it easily. This how I was taught:

QTextStream cout(stdout, QIODevice::WriteOnly);

However, creating a new object is a tad bit more cumbersome than a simple #import <QDebug>. What is the good/least cumbersome way to handle stdout in qt?


Solution

  • The best way is the one you mentioned. You don't have to create a new local variable:

    QTextStream(stdout) << "Hello world!" << Qt::endl;
    

    If the source text is not Latin-1 encoded then you need to convert to QString before passing it to the stream operator:

    QTextStream(stdout) << QString::fromUtf8("utf8 literal") << Qt::endl;