网上已经有很多教程了,这里再说一下:

//注意, mProcess为共享指针 ,要特别注意, 信号和槽的连接要使用原始指针 (也就是mProcess.get()函数)

std::shared_ptr<QProcess> mProcess;    
mProcess = std::make_shared<QProcess>();

mProcess->setProgram(cmd);
mProcess->setArguments(splitProcessCommand(arguments));
mProcess->setProcessChannelMode(QProcess::MergedChannels); //设置读取标准输出模式
mProcess->setProcessEnvironment(env); //设置环境变量
mProcess->setWorkingDirectory(workingDir); //这种工作目录


ok = connect(mProcess.get(), &QProcess::readyReadStandardError,[this](){
this->log(QString::fromLocal8Bit( mProcess->readAllStandardError()));
});

ok = connect(mProcess.get(), &QProcess::readyReadStandardOutput,[this](){
//这样就实现把结果exe的信息给显示在控制台了
this->log(QString::fromLocal8Bit( mProcess->readAllStandardOutput()));
});

mProcess->start();
mProcess->waitForStarted(5000);

//这里可以给它传入一些数据
mProcess->write(readFileToByteArray(mInputFile));
mProcess->closeWriteChannel();


//如果啥时候想终止这个进程,还可以用下面的代码
mProcess->closeReadChannel(QProcess::StandardOutput);
mProcess->closeReadChannel(QProcess::StandardError);
mProcess->closeWriteChannel();
mProcess->terminate();
mProcess->kill();



//这里面就能获得启动的exe的标准输出信息了
void DebugTarget::log(const QString &msg)
{
emit debugTargetOutput(msg);
}

特别注意:

如果需要修改process的控制台窗口是否显示,则用下面的函数


mProcess->setCreateProcessArgumentsModifier([this](QProcess::CreateProcessArguments * args)         //下面这两行让后台exe弹出一个窗口         args->flags |= CREATE_NEW_CONSOLE;         args->flags &= ~CREATE_NO_WINDOW; }


切记: 在这个函数里,下面这一行,千万不要用,否则qprocess 关闭了标准输出 ,我们就获取不到输出了(我 踩了这个巨坑 ,一天才找出来)
args->startupInfo -> dwFlags &= ~STARTF_USESTDHANDLES;

当然,如果不通过信号与槽函数机制,自己 主动去读取mProcee的标准输出 也可以的,那就用

mProcee->readAll()

mProcee->write()