繁体   English   中英

Qt - 显示PowerShell运行QProcess的结果

[英]Qt - Displaying results of PowerShell run through 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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM