简体   繁体   English

在Qt应用程序中运行Windows命令提示符命令

[英]Running windows command prompt commands in a Qt application

I need to run an external exe through Qt application which requires commands to be entered in windows command prompt. 我需要通过Qt应用程序运行一个外部exe,这需要在Windows命令提示符下输入命令。

  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 但是我得到的输出如下普通的cmd提示符

But I want windows command prompt like expected cmd 但是我想要像预期的cmd一样的Windows命令提示符

You need to read from QProcess standart output and print it on screen. 您需要从QProcess标准输出中读取并在屏幕上打印。 You can use pro.waitForReadyRead() and if it returns true do 您可以使用pro.waitForReadyRead() ,如果它返回true,请执行

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. 更好的决定是使用信号插槽机制并实现onReadyToRead()插槽并将QProcess readyReadStandardOutput()信号连接到该插槽。

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

You should not use this two methods at same time, QProcess::execute is static member. 您不应该同时使用这两种方法,QProcess :: execute是静态成员。

You need to start process detached: 您需要启动分离的进程:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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