如何启动控制台进程

时间:2011-05-28 15:52:06

标签: c++ qt console-application qprocess

我正在从我的Windows应用程序运行进程,该进程是控制台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()并且控制台在此之前不会打开。

我该怎么办?

2 个答案:

答案 0 :(得分:0)

尝试system()函数;它将像从windows cmd

运行一样运行命令

答案 1 :(得分:0)

首先The console won't open with QProcess in windows

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()信号,则需要set the read channel。但我建议改用readyReadStandardOutput()信号。