如何启动控制台进程

How to start a console process

本文关键字:控制台 进程 启动 何启动      更新时间:2023-10-16

我正在从我的windows应用程序运行一个进程,该进程是console exe文件。我使用以下代码:

void compilerWindow::runClicked()
{
    proc = new QProcess(this);
    QString name = "C:\qtEcoolCompiler\qt\vm.exe";
    QStringList args = QStringList() << "codeGeneration.vm";
    connect(proc, SIGNAL(readyRead()),
                  SLOT(readFromProc()));
    connect(proc, SIGNAL(error(QProcess::ProcessError)),
                  SLOT(procError(QProcess::ProcessError)));
    connect(proc, SIGNAL(finished(int)),
                  SLOT(procFinished()));
    outputBrowser->clear();
    outputBrowser->append("Begining Of Execution");
    proc->start(name, args);
    proc->waitForFinished();
}

但问题是控制台没有显示(没有打开),并且将调用procFinished(),在此之前控制台不会打开。

我该怎么办?

尝试system()函数;它将像从windows cmd 运行一样运行命令

首先控制台不会在windows 中使用QProcess打开

Note: Windows intentionally suppresses output from GUI-only applications to
inherited consoles. This does not apply to output redirected to files or 
pipes. To forward the output of` GUI-only applications on the console 
nonetheless, you must use SeparateChannels and do the forwarding yourself 
by reading the output and writing it to the appropriate output channels.

因此,您应该使用readAllStandardOutput()或readChannel()或其他提供的函数之一读取进程stdout。我不知道vm.exe做什么,但假设路径是正确的,并且永远不会调用procError(int)。。。。进程正在正确运行和完成。

如果要使用Readyread()信号,则需要设置读取通道。但我建议使用readyReadStandardOutput()信号。