qt printing to terminal

时光怂恿深爱的人放手 提交于 2020-01-30 12:01:41

问题


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!


回答1:


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




回答2:


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

Docs: QDebug




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/17382047/qt-printing-to-terminal

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!