Qt - 显示 PowerShell 通过 QProcess 运行的结果



我正在Qt(v4.7)中从事一个项目,该项目要求我在QProcess中通过Windows PowerShell运行命令。这是我到目前为止所拥有的(使用示例命令):

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,而且我很难找到一种显示过程结果的方法。如果有人有任何建议,将不胜感激。谢谢!

从你的代码开始...

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();

假设你在 MyClass 类中有一个名为 readyToRead() 的插槽,它有一个指向 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
}

最新更新