如何使用Qt打印到控制台 [英] How to print to console when using Qt
问题描述
我使用Qt4和C ++在计算机图形学中制作一些程序。我需要能够在运行时打印一些变量,而不是调试,但 cout
似乎不工作,即使我添加库。
如果它足够打印到 stderr
,您可以使用以下最初用于调试的流:
// qInfo是qt5.5 + 。
qInfo()<< C ++ Style Info Message;
qInfo(C Style Info Message);
qDebug()<< C ++ Style Debug Message;
qDebug(C Style Debug Message);
qWarning()<< C ++ Style Warning Message;
qWarning(C Style Warning Message);
qCritical()<< C ++ Style Critical Error Message;
qCritical(C风格严重错误消息);
// qFatal没有C ++样式方法。
qFatal(C Style Fatal Error Message);
虽然在评论中指出,记住qDebug消息如果 QT_NO_DEBUG_OUTPUT
QTextStream& qStdOut()
{
static QTextStream ts(stdout);
return ts;
}
然后您可以按如下方式调用:
qStdOut()< std out!;
I'm using Qt4 and C++ for making some programs in computer graphics. I need to be able to print some variables in my console at run-time, not debugging, but cout
doesn't seem to work even if I add the libraries. Is there a way to do this?
If it is good enough to print to stderr
, you can use the following streams originally intended for debugging:
//qInfo is qt5.5+ only.
qInfo() << "C++ Style Info Message";
qInfo( "C Style Info Message" );
qDebug() << "C++ Style Debug Message";
qDebug( "C Style Debug Message" );
qWarning() << "C++ Style Warning Message";
qWarning( "C Style Warning Message" );
qCritical() << "C++ Style Critical Error Message";
qCritical( "C Style Critical Error Message" );
// qFatal does not have a C++ style method.
qFatal( "C Style Fatal Error Message" );
Though as pointed out in the comments, bear in mind qDebug messages are removed if QT_NO_DEBUG_OUTPUT
is defined
If you need stdout you could try something like this (as Kyle Strand has pointed out):
QTextStream& qStdOut()
{
static QTextStream ts( stdout );
return ts;
}
You could then call as follows:
qStdOut() << "std out!";
这篇关于如何使用Qt打印到控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!