qt4中怎样获取cout或者printf的输出,然后显示在text上

luoyuan986727 2010-07-22 02:34:07
cout或者printf的输出时直接输出到控制台console上,然后我想在控制台上获取这些输出,并且显示在一个text上,请问各位大侠有什么好方法没有,请说详细点,我才刚学qt不久,谢谢!!
...全文
2391 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
luoyuan986727 2012-01-11
  • 打赏
  • 举报
回复
谢谢咯!
joy_zhou 2011-12-29
  • 打赏
  • 举报
回复
关键是把 printf 重定向,可以在自己的源码中加入以下代码。按 C 或 C++ 连接规则,将连接源码中的已有的 printf 代码,而不连接库中的。这样printf就被重定向到 QTextEdit。

extern QTextEdit *text; //假设:你的文本显示器用 QTextEdit 类
int printf(const char *format,...)
{char buf[1000]; int i; //1000个字节应该足够大,如果不够,可以选用2000甚至更大
va_list vlist;
va_start(vlist,format);
i=vsprintf(buf,format,vlist);
va_end(vlist);
text->append(QString(buf));
return i;
}
yoyowinwin 2010-07-23
  • 打赏
  • 举报
回复


重定向stdout到文件里
luoyuan986727 2010-07-23
  • 打赏
  • 举报
回复
谢谢,我试试
巴依老爷 2010-07-23
  • 打赏
  • 举报
回复 1
试试重定向stdout至文件:
freopen ("myfile.txt","w",stdout);
//调用dll
...
printf ("printf is redirected to a file.\n");
fclose (stdout);
luoyuan986727 2010-07-23
  • 打赏
  • 举报
回复
我现在遇到的问题是,我想调用一个dll,这个dll里面有很多的printf输出,然后我就想获取这些输出,并显示在text里,你们有没有遇到个这个情况,有没有好的方法解决呢??
luoyuan986727 2010-07-23
  • 打赏
  • 举报
回复
谢谢大家!我试试先!!
dext 2010-07-23
  • 打赏
  • 举报
回复
我没有试过我的方法,不过,我看了一下 Qt Creator 的代码,不过要捕捉 其他进程的 可以用

void QProcess::setStandardOutputFile ( const QString & fileName, OpenMode mode = Truncate )
lefttime 2010-07-23
  • 打赏
  • 举报
回复
#6楼提供的方法不错哈~``
dext 2010-07-23
  • 打赏
  • 举报
回复
QTextStream::QTextStream ( FILE * fileHandle, QIODevice::OpenMode openMode = QIODevice::ReadWrite )
Constructs a QTextStream that operates on fileHandle, using openMode to define the open mode. Internally, a QFile is created to handle the FILE pointer.

This constructor is useful for working directly with the common FILE based input and output streams: stdin, stdout and stderr.
Example:

int i = 5;
QTextStream out(stdout);
cout << i;

然后 用 QPlainTextEdit 显示 out
lefttime 2010-07-22
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 luoyuan986727 的回复:]

引用 3 楼 lefttime 的回复:
引用 2 楼 luoyuan986727 的回复:

ReadConsole好像不是qt4里面的类哦,我没见过这个??


这是Win32API~``, 所以说针对于Windows来说的~``
但是怎样才能调用得到ReadConsole函数呢,我每次调用都提示说没有这个函数,我才刚学这个东西,很多东西都还不懂。
[/Quote]

#include <windows.h>
luoyuan986727 2010-07-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lefttime 的回复:]
引用 2 楼 luoyuan986727 的回复:

ReadConsole好像不是qt4里面的类哦,我没见过这个??


这是Win32API~``, 所以说针对于Windows来说的~``
[/Quote]但是怎样才能调用得到ReadConsole函数呢,我每次调用都提示说没有这个函数,我才刚学这个东西,很多东西都还不懂。
lefttime 2010-07-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 luoyuan986727 的回复:]

ReadConsole好像不是qt4里面的类哦,我没见过这个??
[/Quote]

这是Win32API~``, 所以说针对于Windows来说的~``
luoyuan986727 2010-07-22
  • 打赏
  • 举报
回复
ReadConsole好像不是qt4里面的类哦,我没见过这个??
lefttime 2010-07-22
  • 打赏
  • 举报
回复
在Winodws下你可以利用ReadConsole来获取控制台的输出~~`

16,816

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧