2014-04-01 523 views
0

我正在Qt(v4.7)中开发一个项目,这需要我通过QProcess中的Windows PowerShell运行命令。这是我迄今为止(与一个示例命令):Qt - 显示PowerShell运行QProcess的结果

QString path = "C:/Windows/system32/WindowsPowerShell/v1.0/powershell.exe"; 
QStringList commands; 
commands.append("-Command"); 
commands.append("invoke-command -computername mycomputer -credential myuser {ipconfig /all}"); 
QProcess *p = new QProcess(); 
process->start(path, commands); 

这一切似乎工作,并成功运行没有崩溃。现在,我需要能够显示运行此PowerShell命令的结果。我知道当我在cmd窗口中运行它时,它会返回大量数据,但在此之前我根本没有真正使用过QProcess,而且我无法找出显示过程结果的方法。如果有人有任何建议,将不胜感激。谢谢!

+0

“我遇到了麻烦......”你试过了什么? – TheDarkKnight

+0

@ Merlin069我还没有真正尝试过很多,我只是不确定从哪里开始。我在这个类中有一个插槽,它与QProcess的finished()信号相连,因为我想出了任何结果,只有等到过程结束后才能运行结果,而且这是按预期触发的,但是一旦完成获取结果,我找不到任何方法。我一直在浏览文档页面,但找不到任何东西来获取QProcess的结果。 – thnkwthprtls

+0

将插槽连接到readyReadStandardOutput信号并调用readAllStandardOutput怎么样?这是明确记录:http://qt-project.org/doc/qt-5.0/qtcore/qprocess.html – TheDarkKnight

回答

1

与您的代码开始......

QString path = "C:/Windows/system32/WindowsPowerShell/v1.0/powershell.exe"; 
QStringList commands; 
commands.append("-Command"); 
commands.append("invoke-command -computername mycomputer -credential myuser {ipconfig /all}"); 
QProcess *p = new QProcess(); 

假设你有一个名为readyToRead()在MyClass类插槽,其中有一个指向QProcess中,P

connect(p, &QProcess::readyReadStandardOutput, myClass, &MyClass::readyToRead); 
process->start(path, commands); 

然后你我会在槽中收到通知

void MyClass::readyToRead() 
{ 
    QString output(p->readAllStandardOutput()); 

    //Do something with the string 
} 
+0

我不知道我是如何忽略这哈哈,谢谢! – thnkwthprtls

+0

好吧,没问题; O) – TheDarkKnight

+0

我发现了一个小问题:你碰巧知道我需要更改打印PowerShell的结果,当该结果是错误消息?事实证明,我也需要返回它,但现在它不会返回任何东西。我在这里有更多的细节:http://stackoverflow.com/questions/23118530/qt-how-to-output-windows-powershell-error-message-via-qprocess – thnkwthprtls