我正在用Qt4和C++做一些计算机图形学的程序。我需要能够在运行时打印控制台中的一些变量,而不是调试,但是即使我添加库,cout
似乎也不能工作。有没有办法做到这一点?
发布于 2010-10-08 05:47:03
如果可以打印到stderr
,则可以使用以下最初用于调试的流:
#include<QDebug>
//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" );
尽管如注释中所指出的,但请记住,如果定义了QT_NO_DEBUG_OUTPUT
,则会删除qDebug消息
如果你需要stdout,你可以尝试这样做(正如Kyle Strand所指出的):
QTextStream& qStdOut()
{
static QTextStream ts( stdout );
return ts;
}
然后,您可以按如下方式调用:
qStdOut() << "std out!";
发布于 2013-07-15 16:24:46
我发现this最有用:
#include <QTextStream>
QTextStream out(stdout);
foreach(QString x, strings)
out << x << endl;
发布于 2010-10-08 08:58:19
将此代码添加到项目文件中:
CONFIG += console
https://stackoverflow.com/questions/3886105
复制相似问题