如何重定向qml的console.log()到cpp stdout [英] How to redirect qml's console.log() to cpp stdout

查看:1999
本文介绍了如何重定向qml的console.log()到cpp stdout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用qml(qtCreator)和cpp(visual studio)。



通常,错误消息显示在控制台,都来自cpp和qml。 p>

我的要求是我不应该有控制台。



所以我写了一个窗口应用程序。

但是当一个标志被设置,我应该启动一个控制台。



我在函数中使用了以下代码来设置。

  HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); 
int hCrt = _open_osfhandle((long)handle_out,_O_TEXT);
FILE * hf_out = _fdopen(hCrt,w);
setvbuf(hf_out,NULL,_IONBF,128);

//将缓冲区重定向到文件句柄
* stdout = * hf_out;
* stderr = * hf_out;

//将std输入连接到控制台
HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
hCrt = _open_osfhandle((long)handle_in,_O_TEXT);
FILE * hf_in = _fdopen(hCrt,r);
setvbuf(hf_in,NULL,_IONBF,128);
* stdin = * hf_in;

这将从stdout和stderr到控制台打印错误日志。



要重定向qt错误日志,我们可以使用。



如何重定向qDebug,qWarning,qCritical等输出?





提前感谢。

解决方案

在这里,您可以找到更详细的解释。



console.log qDebug ,对最终用途没有区别。你的代码应该工作,所以我想你只是没有正确测试。虽然,您的代码似乎很奇怪,因为它不必要的平台特定。



可能应该摆脱这一点。



顺便说一句,Qt 4正在实现类似于QML查看器。 此处您可以找到代码



希望我的回答能回答您的问题。如果没有,请澄清。


I am using qml( qtCreator ) and cpp (visual studio).

Usually the error messages are shown to the console, both from cpp and qml.

My requirement is that I should not have a console.

So I wrote a window application.

But when a flag has been set, I should launch a console. And show the corresponding error messages there.

I have used the following code in a function to set up this.

HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
int hCrt = _open_osfhandle((long) handle_out, _O_TEXT);
FILE* hf_out = _fdopen(hCrt, "w");
setvbuf(hf_out, NULL, _IONBF, 128);

// redirecting the buffers to the file handle
*stdout = *hf_out;
*stderr = *hf_out;

//attach std input to console
HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
hCrt = _open_osfhandle((long) handle_in, _O_TEXT);
FILE* hf_in = _fdopen(hCrt, "r");
setvbuf(hf_in, NULL, _IONBF, 128);
*stdin = *hf_in;

This will print the error log from stdout and stderr to a console.

To redirect qt error logs we can use.

How to redirect qDebug, qWarning, qCritical etc output?

But how do we redirect the output from console.log() of qml to the console.

Thanks in advance.

解决方案

Here you can find a more verbose explanation.

console.log is simply just qDebug, there is no difference to an end use. Your code should work, so I guess you just have not tested it properly. Although, your code seems to be strange because it is unnecessarily platform specific.

Likely that you should get rid of that.

By the way, Qt 4 was implementing something similar with QML viewer. Here you can find the code for an implementation example, in doubt.

Hope, my reply answers your question. If not, please clarify.

这篇关于如何重定向qml的console.log()到cpp stdout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆