Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 捕获我的程序';s标准输出(以Qt为单位)_C++_Qt_Qstring_Qtcore_Libxslt - Fatal编程技术网

C++ 捕获我的程序';s标准输出(以Qt为单位)

C++ 捕获我的程序';s标准输出(以Qt为单位),c++,qt,qstring,qtcore,libxslt,C++,Qt,Qstring,Qtcore,Libxslt,我正在编写一个使用libxslt库的Qt-GUI应用程序。libxslt总是将错误打印到stderr,但在某些情况下,用户可以看到显示的消息。有没有办法捕获stderr输出并将其放入QString中?有两个快速选项: 1) 使用外部流程包装您的用法并使用 这将引入一个外部包装器。这将不允许您“有时”立即处理错误。你需要一些外部处理 QProcess process; process.start("wrapper"); process.waitForFinished(); qDebug() <

我正在编写一个使用libxslt库的Qt-GUI应用程序。libxslt总是将错误打印到stderr,但在某些情况下,用户可以看到显示的消息。有没有办法捕获stderr输出并将其放入QString中?

有两个快速选项:

1) 使用外部流程包装您的用法并使用

这将引入一个外部包装器。这将不允许您“有时”立即处理错误。你需要一些外部处理

QProcess process;
process.start("wrapper");
process.waitForFinished();
qDebug() << process.readAllStandardError();

您是否尝试过QProcess::readStandardOutput()?这里似乎不适用。OP说他们正在使用一个正在进行中的库。请参阅以了解Qt中解决完全相同问题的人以及更一般性的讨论。@AndrewMedico:您使用过它吗?我不明白OP在哪里提到了“进程中的库”,不管这是什么意思。我认为“编写使用libxslt库的…应用程序”是指他们正在编写一个链接到库中的应用程序,而不是运行子进程。谢谢。我希望避免使用外部流程。如果选项(2)涉及特定于编译器的代码,那么我认为最好创建一个临时文件,然后从中读取。我很高兴知道可能的解决办法。
int pfd[2];
pipe(pfd);
// Do other things here.
close(STDERR_FILENO);
if (pfd[1] != STDERR_FILENO) {
    dup2(pfd[1], STDERR_FILENO);
    close(pfd[1]);
}
read(...);