简体   繁体   English

Qt - 显示PowerShell运行QProcess的结果

[英]Qt - Displaying results of PowerShell run through QProcess

I am working on a project in Qt (v4.7) which requires me to run commands through Windows PowerShell in a QProcess. 我正在开发Qt(v4.7)中的一个项目,该项目要求我在QProcess中通过Windows PowerShell运行命令。 This is what I have so far (with a sample command): 这是我到目前为止(使用示例命令):

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

This all seems to work and run successfully without crashing. 这一切似乎都可以正常运行而不会崩溃。 Now, I need to be able to display the results of running this PowerShell command. 现在,我需要能够显示运行此PowerShell命令的结果。 I know when I run it in the cmd window it returns a lot of data, but I haven't really used QProcess at all before this, and I'm having trouble figuring out a way to show the results of the process. 我知道当我在cmd窗口中运行它时会返回大量数据,但在此之前我还没有真正使用过QProcess,而且我很难找到一种方法来显示该过程的结果。 If anyone has any advice it would be greatly appreciated. 如果有人有任何建议,将不胜感激。 Thanks! 谢谢!

Starting with your code... 从你的代码开始......

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

Assuming you have a slot called readyToRead() in a class MyClass, which has a pointer to the QProcess, p 假设你在MyClass类中有一个名为readyToRead()的槽,它有一个指向QProcess的指针,p

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

Then you'll receive notification in the slot 然后你会在插槽中收到通知

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