如何在QProcess中使用cmd.exe命令



我尝试在Windows命令提示符中运行命令类型,但进程并没有启动。如果我在命令提示符下尝试使用这个命令,它会起作用。这是代码:

void MainWindow::firmware()
{
QProcess *proc = new QProcess(this);
QString dir("C:/Qtstatic/Examples/My/build-RBoxReader-Desktop_Qt_5_15_0_MSVC2019_64bit-Debug");
proc->setWorkingDirectory(dir);
QMessageBox msgBox;
QString msg = "Current dir: %1";
msg = msg.arg(proc->workingDirectory());
msgBox.setText(msg);
msgBox.exec();
QString command("type");
QStringList arguments;
arguments << "my_file.txt";
proc->start(command, arguments);
if(!proc->waitForStarted()) {
QMessageBox msgBox1;
QString msg1 = "Process state: %1";
msg1 = msg1.arg(proc->state());
msgBox1.setText(msg1);
msgBox1.exec();
QMessageBox msgBox;
QString msg = "Process didn't start";
msgBox.setText(msg);
msgBox.exec();
return;
}
msg = "Process state: %1";
msg = msg.arg(proc->state());
msgBox.setText(msg);
msgBox.exec();
proc->waitForFinished (-1);
}

鉴于在您的代码中没有看到"cmd.exe",我无法理解您的期望。您的系统上没有type.exe。您必须自己实际运行cmd.exe,并将type作为参数传递给它:

你会想要这样的东西:

QString command = QStringLiteral("cmd.exe");
QStringList arguments{QStringLiteral("/K"),
QStringLiteral("type"),
QStringLiteral("my_file.txt")};

相关内容

  • 没有找到相关文章

最新更新