C++ 使用Qt时如何打印到控制台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3886105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:58:23  来源:igfitidea点击:

How to print to console when using Qt

c++qtcout

提问by lesolorzanov

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 coutdoesn't seem to work even if I add the libraries. Is there a way to do this?

我正在使用 Qt4 和 C++ 来制作一些计算机图形程序。我需要能够在运行时在我的控制台中打印一些变量,而不是调试,但cout即使我添加了库似乎也不起作用。有没有办法做到这一点?

回答by Goz

If it is good enough to print to stderr, you can use the following streams originally intended for debugging:

如果打印到 足够好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" );

Though as pointed out in the comments, bear in mind qDebug messages are removed if QT_NO_DEBUG_OUTPUTis defined

尽管正如评论中所指出的,请记住,如果QT_NO_DEBUG_OUTPUT定义了qDebug 消息,则会删除

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!";

回答by CapelliC

I found thismost useful:

我发现最有用:

#include <QTextStream>

QTextStream out(stdout);
foreach(QString x, strings)
    out << x << endl;

回答by Kyle Strand

Writing to stdout

写信给 stdout

If you want something that, like std::cout, writes to your application's standard output, you can simply do the following(credit to CapelliC):

如果你想要一些东西,比如std::cout,写到你的应用程序的标准输出,你可以简单地执行以下操作归功于 CapelliC):

QTextStream(stdout) << "string to print" << endl;

If you want to avoid creating a temporary QTextStreamobject, follow Yakk's suggestion in the comments below of creating a function to return a statichandle for stdout:

如果您想避免创建临时QTextStream对象,请遵循 Yakk 在下面的注释中的建议,创建一个函数来返回 的static句柄stdout

inline QTextStream& qStdout()
{
    static QTextStream r{stdout};
    return r;
}

...

foreach(QString x, strings)
    qStdout() << x << endl;


Rememberto flushthe stream periodically to ensure the output is actually printed.

记得flush流周期性地,以确保实际打印输出。

Writing to stderr

写信给 stderr

Note that the above technique can also be used for other outputs. However, there are more readable ways to write to stderr(credit to Gozand the comments below his answer):

请注意,上述技术也可用于其他输出。但是,有更多可读的方式来写stderr归功于 Goz和他的回答下面的评论):

qDebug() << "Debug Message";    // CAN BE REMOVED AT COMPILE TIME!
qWarning() << "Warning Message";
qCritical() << "Critical Error Message";
qFatal("Fatal Error Message");  // WILL KILL THE PROGRAM!

qDebug()is closed if QT_NO_DEBUG_OUTPUTis turned on at compile-time.

qDebug()如果QT_NO_DEBUG_OUTPUT在编译时打开则关闭。

(Goz notes in a comment that for non-console apps, these can print to a different stream than stderr.)

(Goz 在评论中指出,对于非控制台应用程序,它们可以打印到与 不同的流stderr。)



NOTE:All of the Qt print methods assume that const char*argumentsare ISO-8859-1 encoded strings with terminating \0characters.

注意:所有 Qt 打印方法都假定const char*参数是带有终止\0字符的ISO-8859-1 编码字符串。

回答by Kyle Lutz

Add this to your project file:

将此添加到您的项目文件中:

CONFIG += console

回答by Sebastian Negraszus

What variables do you want to print? If you mean QStrings, those need to be converted to c-Strings. Try:

你想打印什么变量?如果您指的是 QStrings,则需要将其转换为 c-Strings。尝试:

std::cout << myString.toAscii().data();

回答by ulitosCoder

It also has a syntax similar to prinft, e.g.:

它也有一个类似于 prinft 的语法,例如:

qDebug ("message %d, says: %s",num,str); 

Very handy as well

也很方便

回答by Son Vu

Go the Project's Properties -> Linker-> System -> SubSystem, then set it to Console(/S).

转到项目的Properties -> Linker-> System -> SubSystem,然后将其设置为Console(/S)

回答by Emerald Cottet

What about including iostream libraryand precise that coutis an object of stdlike this :

如何包括iostream 库和精确的cout是这样的std对象:

#include <iostream>

std::cout << "Hello" << std::endl;

回答by Amir Twito

#include <QTextStream>
...
qDebug()<<"Bla bla bla";

回答by Andrew Prock

If you are printing to stderr using the stdio library, a call to fflush(stderr)should flush the buffer and get you real-time logging.

如果您使用 stdio 库打印到 stderr,则调用fflush(stderr)应刷新缓冲区并获得实时日志记录。