Running windows command prompt commands in a Qt ap

2019-09-06 07:21发布

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

2条回答
Deceive 欺骗
2楼-- · 2019-09-06 07:46

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.

查看更多
Emotional °昔
3楼-- · 2019-09-06 08:06
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
查看更多
登录 后发表回答