1

In my QT application, I want to have it so some real-time information prints in the terminal if I run the application from the terminal.

When I use printf("print this") (either in main or during the paint event), it doesn't print until I close the gui.

Why is this, and how can I have it print information in real-time? (I'm using linux)

Thanks!

Nathan
  • 73,987
  • 14
  • 40
  • 69

4 Answers4

3

To write to stdout, you should add this CONFIG += console to your project file config and use cout of printf for your liking. qDebug prints by default to stderr. Check this topic for more info - How to print to console when using Qt

Community
  • 1
  • 1
Shf
  • 3,463
  • 2
  • 26
  • 42
  • I'm pretty sure the default message handler sends output to stdout. I use it that way all the time. – Jacob Robbins Jun 29 '13 at 16:57
  • @JacobRobbins Yes, but unless `CONFIG += console` is added, it will not be shown, except for qDebug, becouse it sends to stderr i guess – Shf Jun 29 '13 at 17:00
  • CONFIG += console is only needed on Windows. On Unix, printf, qDebug etc. are always printed to the console (if at all, i.e. qdebug isn't disabled) – Frank Osterfeld Jun 29 '13 at 20:13
1

You can use qDebug() << ..., qWarning() << ..., etc. Don't forget to include <QDebug>.

Docs: QDebug

Jacob Robbins
  • 1,860
  • 14
  • 16
1

Qt doesn't interfere with printf output. (On Windows qmake (not Qt) does, but that doesn't apply to Linux). However, consider that the buffering behavior for stdout leads to printf("print this") not being printed until the buffer is flushed. Try with e.g. fflush(stdout) or simply append a newline: printf("print this\n") to have the buffer flushed. That's not related to Qt at all though.

Frank Osterfeld
  • 24,815
  • 5
  • 58
  • 70
0

You may also want to try adding CONFIG -= app_bundle to your .pro file.

Jason deMello
  • 35
  • 1
  • 7