4

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?

Anon
  • 2,267
  • 3
  • 34
  • 51

2 Answers2

8

qDebug(), qInfo(), etc. are all piped to a default message handler. But you can easily install your own that writes the debug strings to a different stream, file, or anything. All you need to do is define a message handler function and install it using qInstallMessageHandler().

Putting it all together, here's a complete example:

#include <QDebug>

void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    QTextStream cout(stdout, QIODevice::WriteOnly);
    cout << msg << endl;
}

int main(int argc, char *argv[])
{
    qInstallMessageHandler(myMessageOutput);

    qDebug().noquote() << "Hello world!";
}
MrEricSir
  • 8,044
  • 4
  • 30
  • 35
3

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;
Jason Haslam
  • 2,617
  • 13
  • 19
  • I'd accept this, but `QTextStream(stdout)` does not support unicode. Any way to fix this? – Anon Mar 20 '17 at 01:44
  • What do you mean by "does not support unicode"? `QTextStream` converts from `QString` (UTF-16) to the default encoding for your locale (`QTextCodec::codecForLocale`) by default or any other encoding of your choosing (`QTextStream::setCodec`). – Jason Haslam Mar 20 '17 at 02:51
  • What I mean, is that my unicode is not displaying properly in my shell where as they would under `qDebug() << "≢≢≢";` – Anon Mar 20 '17 at 03:06
  • 2
    I suppose that the difference is that the `QTextStream::<<` overload that takes a `const char *` converts the input stream from Latin-1 while the `QDebug` overload must just pass that string straight through to the backend. So if your source file is UTF-8 encoded then you would have to use something like `QString::fromUtf8("utf8 text")` or just `QString("utf8 text")`. – Jason Haslam Mar 20 '17 at 03:25
  • Welp; any way around this? – Anon Mar 20 '17 at 03:26
  • 1
    Sorry, I hit enter too soon. The workaround is to covert from your source encoding to `QString` before passing it to the stream operator. I'll update the answer accordingly. – Jason Haslam Mar 20 '17 at 03:29
  • @JasonHaslam this is where the `QString::fromXXX()` methods come in handy. – Steve Folly Mar 11 '20 at 16:16
  • So if I use this everywhere I want output, it will instantiate a new QTextStream everytime, won't it? – Johannes S. Jun 09 '21 at 13:51
  • @JohannesS. Yes – Jason Haslam Jun 09 '21 at 18:18