2009-08-13 54 views
7

我一直在windows上学习qt一段时间(unix/embedded中的背景),并希望从我的win32 qt GUI应用程序中将stderr/stdout转出某处(单元测试/事件日志记录/调试)。这在Windows和I found this post on stackoverflow这似乎是一个很高的顺序,这解释了为什么。从qt创建者启动的应用程序的stdout/cout?

我发现自己想知道为什么QT没有一个简单的机制来执行调试构建的帖子中的一些建议。

这样的设施是否已经在qt中存在,或者我是否离开我自己(或找到syslog lib)?

回答

14

qDebug()和相关功能时很方便,诸如此类的事情 - 将被发送到调试器

#include <QDebug> 

qDebug() << "x is: " << x; 
+0

这正是我期待的,谢谢! – tim 2009-08-13 22:18:56

2

您可以随时启动(如果你使用Qt Creator的,这将挑选那些起来很方便!)你的程序从命令行看到stdout输出(cmd.exe)。 另外,就像Paul Dixon所说,通过使用qDebug(),您应该能够在调试器中看到输出。

#include <QDebug> 
... 
{ 
    ... 
    int x = 5; 
    qDebug() << "x is: " << x; 
} 
+0

感谢您的代码片段,这很有帮助。 – tim 2009-08-13 22:20:37

2

一种廉价的方式是简单地重新打开标准输出/ ERR(在Win32以及ATLEAST,我假设它会使用Qt以及工作)

#include <stdio> 

//add this at the beginning of your main 
freopen("c:\\temp\\stdout.txt","w",stdout); 
freopen("c:\\temp\\stderr.txt","w",stderr); 

如果你需要一些更严重的追踪/记录考虑例如log4cxx

+0

感谢您的指点,我有我自己的日志记录基础设施,我发明了,看起来很像log4cxx。我将不得不仔细看看那个,看看我是否应该使用它。 – tim 2009-08-13 22:20:03

0

我在Tools -> Options -> Build & Run -> [X] Merge stderr and stdout下找到了一个设置。

这将帮助发送到cout的东西被显示。