出于好奇,我玩弄了QProcess。
我真的很佩服一切工作的简单和直接(恐惧地记得过去我们在没有 Qt 的情况下做这件事是多么困难)。
因此,我可以提供我的小rather MCVE 进行演示。
首先我做了一个简单的控制台应用testQProcessIOChild.cc:
#include <chrono>
#include <iostream>
#include <sstream>
#include <string>
#include <thread>
using namespace std;
// inspired by:
// https://try.kotlinlang.org/#/Examples/Longer%20examples/99%20Bottles%20of%20Beer/99%20Bottles%20of%20Beer.kt
string bottlesOfBeer(int n)
{
switch (n) {
case 0: return "no more bottles of beer";
case 1: return "1 bottle of beer";
default: {
ostringstream outFmt;
outFmt << n << " bottles of beer";
return outFmt.str();
}
}
}
int main()
{
enum { delay = 1000 };
for (int n;;) {
cout << "Initial number of bottles (-1 ... finish): " << flush;
if (!(cin >> n)) {
cerr << "Input error!" << endl;
continue;
}
if (n < -1) {
cerr << "Illegal input!" << endl;
continue;
}
if (n < 0) break;
if (n > 100) {
cerr << "The ministry of health warns:" << endl
<< " Abuse of alcoholics may damage your health." << endl;
n = 99;
}
cout << "Go to the store and buy some more, "
<< bottlesOfBeer(n) << " on the wall." << endl;
while (n) {
this_thread::sleep_for(chrono::milliseconds(delay));
cout << bottlesOfBeer(n) << " on the wall, "
<< bottlesOfBeer(n) << '.' << endl
<< "Take one down, pass it around, ";
--n;
cout << bottlesOfBeer(n) << " on the wall." << endl;
}
this_thread::sleep_for(chrono::milliseconds(delay));
cout << "No more bottles of beer on the wall, no more bottles of beer."
<< endl;
}
return 0;
}
我这样做是为了提供一些东西:
- 标准输入的使用
- 标准输出的使用
- 标准错误的使用
- 某些特定的耗时行为(查看父进程是否以及何时做出反应)。
第二次我将 Qt GUI 应用程序testQProcessIO.cc 作为包装器:
// Qt header:
#include <QtWidgets>
const char *childProgram = "./testQProcessIOChild";
int main(int argc, char **argv)
{
qDebug() << QT_VERSION_STR;
// main application
QApplication app(argc, argv);
QProcess qProcessChild;
// GUI setup
QWidget qWin;
QGridLayout qGrid;
QPushButton qBtnStart(QString::fromUtf8("Start"));
qGrid.addWidget(&qBtnStart, 0, 0);
QPushButton qBtnStop(QString::fromUtf8("Stop"));
qBtnStop.setEnabled(false);
qGrid.addWidget(&qBtnStop, 0, 1);
QLabel qLblInput(QString::fromUtf8("Input: "));
qLblInput.setEnabled(false);
qGrid.addWidget(&qLblInput, 0, 2);
QLineEdit qInput;
qInput.setEnabled(false);
qGrid.addWidget(&qInput, 0, 3);
QTextEdit qTxtLog;
qTxtLog.setReadOnly(true);
qGrid.addWidget(&qTxtLog, 1, 0, 1, 4);
qGrid.setRowStretch(1, 1);
qGrid.setColumnStretch(3, 1);
qWin.setLayout(&qGrid);
qWin.show();
// install signal handlers
QObject::connect(&qBtnStart, &QPushButton::clicked,
[&](bool) {
qProcessChild.start(QString::fromLatin1(childProgram));
});
QObject::connect(&qBtnStop, &QPushButton::clicked,
[&](bool) {
qProcessChild.kill();
});
QObject::connect(&qInput, &QLineEdit::returnPressed,
[&](){
QString text = qInput.text() + '\n';
qProcessChild.write(text.toLatin1());
});
QObject::connect(&qProcessChild, &QProcess::started,
[&]() {
qBtnStart.setEnabled(false);
qBtnStop.setEnabled(true);
qLblInput.setEnabled(true);
qInput.setEnabled(true);
});
QObject::connect(&qProcessChild,
// cast needed because QProcess::finished() is polymorph
(void(QProcess::*)(int))&QProcess::finished,
[&](int) {
qBtnStart.setEnabled(true);
qBtnStop.setEnabled(false);
qLblInput.setEnabled(false);
qInput.setEnabled(false);
qTxtLog.clear();
});
QObject::connect(&qProcessChild, &QProcess::readyReadStandardOutput,
[&]() {
qTxtLog.append(qProcessChild.readAllStandardOutput());
});
QObject::connect(&qProcessChild, &QProcess::readyReadStandardError,
[&]() {
qTxtLog.append(qProcessChild.readAllStandardError());
});
// run application
return app.exec();
}
我在 Windows 10(64 位)上使用 VS2013 和 Qt 5.9.2 编译并测试了它。
为了说明测试会话,我后来写了一个 QMake 项目testQProcessIO.pro:
SOURCES = testQProcessIO.cc
QT += widgets
再次在cygwin上编译测试:
$ g++ -std=c++11 -o testQProcessIOChild testQProcessIOChild.cc
$ ./testQProcessIOChild
Initial number of bottles (-1 ... finish): -1
$ qmake-qt5 testQProcessIO.pro
$ make
g++ -c -fno-keep-inline-dllexport -D_GNU_SOURCE -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I. -isystem /usr/include/qt5 -isystem /usr/include/qt5/QtWidgets -isystem /usr/include/qt5/QtGui -isystem /usr/include/qt5/QtCore -I. -I/usr/lib/qt5/mkspecs/cygwin-g++ -o testQProcessIO.o testQProcessIO.cc
g++ -o testQProcessIO.exe testQProcessIO.o -lQt5Widgets -lQt5Gui -lQt5Core -lGL -lpthread
$ ./testQProcessIO
5.9.2
$
我的测试会话快照: