Qt - 如何通过 QProcess 输出 Windows PowerShell 错误消息 [英] Qt - How to output Windows PowerShell error message via QProcess

查看:57
本文介绍了Qt - 如何通过 QProcess 输出 Windows PowerShell 错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Qt 4.7 中工作,我有一个程序需要使用 QProcess 来输出运行 Windows PowerShell 命令的结果.出于这个问题的目的,假设所有需要支持的是使用-Command"选项.现在我有这个:

I am working in Qt 4.7, and I have a program that needs to use a QProcess to output the result of running a Windows PowerShell command. For the purposes of this question, let's say all that needs to be supported is use of the "-Command" option. Right now I have this:

QString path = "C:/windows/system32/WindowsPowerShell/v1.0/powershell.exe";QStringList 命令;command.append("-Command");command.append(/*无论我想使用什么测试命令...*/);进程 = 新 QProcess();//注意:QProcess *process 是这个类的成员connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(/*slot to print qprocess errors...*/);connect(process, SIGNAL(readyReadStandardOutput()), this, SLOT(/*slot to display PowerShell output...*/);进程->开始(路径,命令);

打印 PowerShell 输出的插槽如下:

The slot to print the PowerShell output is simply as follows:

std::cout <<结果:"<<QString(process->readAllStandardOutput()).toStdString() <<std::endl;

这与正确的 PowerShell 命令完美配合.例如,我使用命令Get-ChildItem C:\"对其进行了测试,它打印了正确的数据.如果存在 QProcess 错误,它也可以正常工作.我需要知道如何做的是,如何让它打印 PowerShell 错误消息?例如,如果我尝试直接在 PowerShell 中使用命令Get-ChildIte"(最后缺少 m),我会收到一条错误消息.但是使用我的代码,它只是不打印任何内容.我需要它来打印该错误消息.如果有人知道可以做到这一点的方法,我将不胜感激.谢谢!

This works perfectly with correct PowerShell commands. For example, I tested it with the command "Get-ChildItem C:\", and it printed the correct data. It also works fine if there is a QProcess error. What I need to know how to do is, how can I have it print a PowerShell error message? For example, if I try to use the command "Get-ChildIte" (missing the m at the end) directly in PowerShell I get an error message. But with my code, it just doesn't print anything. I need it to print that error message. If anyone knows of a way this could be done, I'd really appreciate it. Thanks!

推荐答案

所以,在我发布这篇文章大约一分钟后,我发现了 QProcess 的信号 readyReadStandardError(),它的操作与 readyReadStandardOutput 除了当 QProcess 正在运行的任何内容出现错误时都会发出它.我将它连接到与上面相同的插槽,但打印 QString(process->readAllStandardError()).toStdString() 并且它工作了.

So, about a minute after I posted this I discovered QProcess's signal readyReadStandardError(), which operates identically to readyReadStandardOutput except that it is emitted when whatever the QProcess is running has an error. I connected this to a slot identical to what I have above but instead printing QString(process->readAllStandardError()).toStdString() and it worked.

这篇关于Qt - 如何通过 QProcess 输出 Windows PowerShell 错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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