【发布时间】:2014-04-16 19:31:26
【问题描述】:
我正在使用 Qt 4.7,并且我有一个程序需要使用 QProcess 来输出运行 Windows PowerShell 命令的结果。就这个问题而言,假设所有需要支持的是使用“-Command”选项。现在我有这个:
QString path = "C:/windows/system32/WindowsPowerShell/v1.0/powershell.exe";
QStringList command;
command.append("-Command");
command.append(/*Whatever test command I want to use...*/);
process = new QProcess(); //Note: QProcess *process is a member of this class
connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(/*slot to print qprocess errors...*/);
connect(process, SIGNAL(readyReadStandardOutput()), this, SLOT(/*slot to display PowerShell output...*/);
process->start(path, command);
打印PowerShell输出的槽如下:
std::cout << "RESULT: " << QString(process->readAllStandardOutput()).toStdString() << std::endl;
这与正确的 PowerShell 命令完美配合。例如,我使用命令“Get-ChildItem C:\”对其进行了测试,它打印了正确的数据。如果出现 QProcess 错误,它也可以正常工作。我需要知道如何做的是,如何让它打印 PowerShell 错误消息?例如,如果我尝试直接在 PowerShell 中使用命令“Get-ChildIte”(最后缺少 m),我会收到一条错误消息。但是使用我的代码,它不会打印任何内容。我需要它来打印该错误消息。如果有人知道可以做到这一点的方法,我将不胜感激。谢谢!
【问题讨论】:
标签: c++ windows qt powershell