使用Qt时如何打印到控制台
我正在使用 Qt4 和 C++ 来制作一些计算机图形学程序。我需要能够在运行时在控制台中打印一些变量,而不是调试,但即使我添加了库,cout
似乎也不起作用。有办法做到这一点吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
如果打印到
stderr
足够好,您可以使用以下最初用于调试的流:尽管如注释中所指出,但请记住,如果
QT_NO_DEBUG_OUTPUT
, qDebug 消息将被删除code> 已定义如果您需要标准输出,您可以尝试类似的操作(正如凯尔·斯特兰德指出的那样):
然后您可以按如下方式调用:
If it is good enough to print to
stderr
, you can use the following streams originally intended for debugging:Though as pointed out in the comments, bear in mind qDebug messages are removed if
QT_NO_DEBUG_OUTPUT
is definedIf you need stdout you could try something like this (as Kyle Strand has pointed out):
You could then call as follows:
我发现这个最有用:
I found this most useful:
将其添加到您的项目文件中:
Add this to your project file:
写入
stdout
如果您想要将
std::cout
等内容写入应用程序的标准输出,您只需执行 以下内容 (归功于 CapelliC):如果您想避免创建临时
QTextStream
对象,请按照 Yakk 在下面的注释中的建议创建一个函数以返回static<
stdout
的 /code> 句柄:记住定期
刷新
流以确保实际打印输出。写入
stderr
请注意,上述技术也可用于其他输出。然而,还有更易读的方式来写入
stderr
(归功于 Goz 和下面的评论他的答案):如果在编译时打开
QT_NO_DEBUG_OUTPUT
,则qDebug()
将关闭。(Goz 在评论中指出,对于非控制台应用程序,这些应用程序可以打印到与
stderr
不同的流。)注意: 所有 Qt 打印方法 假设
const char*
参数是 ISO-8859-1 编码的字符串,以\0
字符结尾。Writing to
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):If you want to avoid creating a temporary
QTextStream
object, follow Yakk's suggestion in the comments below of creating a function to return astatic
handle forstdout
:Remember to
flush
the stream periodically to ensure the output is actually printed.Writing to
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 Goz and the comments below his answer):qDebug()
is closed ifQT_NO_DEBUG_OUTPUT
is turned on at compile-time.(Goz notes in a comment that for non-console apps, these can print to a different stream than
stderr
.)NOTE: All of the Qt print methods assume that
const char*
arguments are ISO-8859-1 encoded strings with terminating\0
characters.你想打印什么变量?如果您指的是 QString,则需要将其转换为 c-String。尝试:
What variables do you want to print? If you mean QStrings, those need to be converted to c-Strings. Try:
它还有类似于 printft 的语法,例如:
也非常方便
It also has a syntax similar to prinft, e.g.:
Very handy as well
转到项目的
Properties ->;链接器->系统-> SubSystem
,然后将其设置为Console(/S)
。Go the Project's
Properties -> Linker-> System -> SubSystem
, then set it toConsole(/S)
.包含 iostream 库 并精确地说 cout 是 std 的对象怎么样:
What about including iostream library and precise that cout is an object of std like this :
如果您使用 stdio 库打印到 stderr,则调用 fflush(stderr) 应该刷新缓冲区并获得实时日志记录。
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.好吧,在研究了互联网上描述如何从 Qt 中的 GUI 向 stdout 输出消息的几个示例之后,我改进了一个通过 qDebug() 和安装 qInstallMessageHandler() 将消息重定向到控制台的独立工作示例。控制台将与 GUI 同时显示,并且可以在必要时隐藏。该代码很容易与项目中的现有代码集成。这是完整的示例,只要您遵守 GNU GPL v2 许可证,您可以随意以任何方式使用它。我认为你必须使用某种形式和一个 MainWindow - 否则示例将运行,但在被迫退出时可能会崩溃。注意:无法通过关闭按钮或菜单关闭来退出,因为我已经测试了这些替代方案,并且应用程序最终会时不时地崩溃。如果没有关闭按钮,应用程序将保持稳定,您可以从主窗口将其关闭。享受!
Well, after studying several examples on the Internet describing how to output messages from a GUI in Qt to stdout, I have refined a working stand-alone example on redirecting messages to a console, via qDebug() and installing qInstallMessageHandler(). The console will be showed at the same time as the GUI and can be hidden if deemed necessary. The code is easy to integrate with existing code in your project. Here is the full sample and feel free to use it in any way as you like, as long as you adhere to the License GNU GPL v2. You have to use a form of some sort and a MainWindow I think - otherwise the sample will run, but probably crash when forced to quit. Note: there is no way to quit via a close button or a menu close because I have tested those alternatives and the application will crash eventually every now and then. Without the close button the application will be stable and you can close it down from the main window. Enjoy!
“构建并运行”>默认为“在终端中运行”--> 使用此命令启用
刷新缓冲区 --> fflush(标准输出);
您还可以在
printf
或cout
中使用“\n”。"build & run" > Default for "Run in terminal" --> Enable
to flush the buffer use this command --> fflush(stdout);
you can also use "\n" in
printf
orcout
.对于Qt5 QString必须进行转换,例如:
For Qt5 QString must be converted, for example: