系统环境:Windows 11, Qt 5.15.2, Mingw-w64 13.1.0
原本的代码:
mProcess = new QProcess(this);
mProcess->setProgram("wget.exe");
mProcess->setArguments(
{"xxxxx"});
connect(mProcess, &QProcess::readyRead,
[this] { qDebug().noquote() << mProcess->readAll(); });
mProcess->start();
mProcess->waitForReadyRead();
mProcess->waitForFinished();
排查了半天,愣是发现不了哪里出问题……
一看 Qt Assistant,一段话突然引起了注意
翻译:Windows有意抑制从仅GUI应用程序到继承控制台的输出。这不适用于重定向到文件或管道的输出。然而,要在控制台上转发仅GUI应用程序的输出,您必须使用SeparateChannels,并通过读取输出并将其写入适当的输出通道来自己进行转发。
明摆着要改 processChannelMode,于是我向代码中添加了一行
mProcess->setProcessChannelMode(QProcess::SeparateChannels);
带着激动的心情编译、运行……还是不行?
想着硬碰硬,大不了把所有的 processChannelMode 全试一遍,结果发现设为 MergedChannels 就好了。
mProcess->setProcessChannelMode(QProcess::MergedChannels);
修改后的代码:
mProcess = new QProcess(this);
mProcess->setProgram("wget.exe");
mProcess->setArguments(
{"xxxxx"});
mProcess->setProcessChannelMode(QProcess::MergedChannels);
connect(mProcess, &QProcess::readyRead,
[this] { qDebug().noquote() << mProcess->readAll(); });
mProcess->start();
mProcess->waitForReadyRead();
mProcess->waitForFinished();