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

标签 c++ windows qt powershell

我正在使用 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
}

关于c++ - Qt - 显示通过 QProcess 运行的 PowerShell 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22789178/

相关文章:

c++ - CMake "undefined reference to function"

java - 对 Queap 数据结构的插入操作

c - Qt 中对 NetWkstaUserGetInfo 的 undefined reference ,但在 VS 中有效

windows - 从嵌套的批处理文件中退出

Qt4关闭和销毁的区别?

qt - 在 Qt 中发布事件而不指定目标对象

java - 如何确保只有我的原始客户端用于向我的服务器发出请求?

c++ - Windows C++ 间歇性套接字断开连接

c++ - Qt 应用程序在退出时挂起(在 QDialog 执行之后)

c++ - 两个数组之间最近点的索引