简体   繁体   中英

Qt - How to output Windows PowerShell error message via QProcess

I am working in Qt 4.7, and I have a program that needs to use a QProcess to output the result of running a Windows PowerShell command. For the purposes of this question, let's say all that needs to be supported is use of the "-Command" option. Right now I have this:

QString path = "C:/windows/system32/WindowsPowerShell/v1.0/powershell.exe"; QStringList command; command.append("-Command"); command.append(/*Whatever test command I want to use...*/); process = new QProcess(); //Note: QProcess *process is a member of this class connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(/*slot to print qprocess errors...*/); connect(process, SIGNAL(readyReadStandardOutput()), this, SLOT(/*slot to display PowerShell output...*/); process->start(path, command);

The slot to print the PowerShell output is simply as follows:

std::cout << "RESULT: " << QString(process->readAllStandardOutput()).toStdString() << std::endl;

This works perfectly with correct PowerShell commands. For example, I tested it with the command "Get-ChildItem C:\\", and it printed the correct data. It also works fine if there is a QProcess error. What I need to know how to do is, how can I have it print a PowerShell error message? For example, if I try to use the command "Get-ChildIte" (missing the m at the end) directly in PowerShell I get an error message. But with my code, it just doesn't print anything. I need it to print that error message. If anyone knows of a way this could be done, I'd really appreciate it. Thanks!

Ok, you posted your answer 7 seconds ago. I just wanted to confirm, and give a link to reference for you that may help troubleshoot this in the future.

QProcess Class

Yes, you want the readAllStandardError() function of QProcess.

So, about a minute after I posted this I discovered QProcess's signal readyReadStandardError() , which operates identically to readyReadStandardOutput except that it is emitted when whatever the QProcess is running has an error. I connected this to a slot identical to what I have above but instead printing QString(process->readAllStandardError()).toStdString() and it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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