Running windows command prompt commands in a Qt application

梦想的初衷 提交于 2020-01-17 04:34:04

问题


I need to run an external exe through Qt application which requires commands to be entered in windows command prompt.

  QString exePath = "C:\Windows\system32\cmd.exe";
  QProcess pro;
  pro.start(exePath);
  pro.execute("cmd.exe");

But I got output like below plain cmd prompt

But I want windows command prompt like expected cmd


回答1:


You need to read from QProcess standart output and print it on screen. You can use pro.waitForReadyRead() and if it returns true do

QByteArray arr = pro.readAllStandardOutput();
QString str(arr);
qDebug() << str;

Better decision is to use signal slot mechanism and implement onReadyToRead() slot and connect QProcess readyReadStandardOutput() signal to it.




回答2:


pro.start(exePath);
pro.execute("cmd.exe");

You should not use this two methods at same time, QProcess::execute is static member.

You need to start process detached:

QString exePath = "C:\Windows\system32\cmd.exe";
QProcess pro;
pro.startDetached(exePath);
pro.waitForStarted();
//Event Loop here


来源:https://stackoverflow.com/questions/36053351/running-windows-command-prompt-commands-in-a-qt-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!