1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| #include <QtGui>
#include <iostream>
#include <io.h>
#include <fcntl.h>
static QString getCin()
{
std::string s;
std::getline(std::cin,s);
return QString::fromStdString(s);
}
class myWidget : public QWidget
{
Q_OBJECT
QLineEdit * lineEdit;
QTextBrowser * browser;
QFutureWatcher<QString> watcher;
QProcess myProcess;
bool childProcess;
public:
myWidget(bool b)
:childProcess(b)
{
QVBoxLayout * l = new QVBoxLayout(this);
lineEdit = new QLineEdit;
browser = new QTextBrowser;
l->addWidget(lineEdit);
l->addWidget(browser);
if (childProcess)
{
setWindowTitle("child process");
connect(lineEdit,SIGNAL(returnPressed ()),this,SLOT(sendTostdout()));
}
else
{
setWindowTitle("parent process");
connect(&myProcess,SIGNAL(readyReadStandardOutput ()),this,SLOT(processend()));
connect(lineEdit,SIGNAL(returnPressed ()),this,SLOT(sendToProcess()));
QStringList args;
args<<"s";
QString s = QCoreApplication::applicationFilePath () ;
myProcess.start(QCoreApplication::applicationFilePath () ,args);
}
connect(&watcher,SIGNAL(finished ()),this,SLOT(watcherend()));
QTimer::singleShot(0,this,SLOT(launchFuture()));
}
public slots:
void launchFuture()
{
watcher.setFuture(QtConcurrent::run(getCin));
}
void watcherend()
{
browser->append(watcher.result());
watcher.setFuture(QtConcurrent::run(getCin));
}
void sendTostdout()
{
std::cout<<lineEdit->text().toStdString()<<std::endl;
fflush(stdout);
}
void processend()
{
browser->append(myProcess.readAllStandardOutput());
}
void sendToProcess()
{
myProcess.write(lineEdit->text().toLocal8Bit());
myProcess.putChar('\n');
}
};
#include "main.moc"
int main(int argc, char **argv) {
QApplication app(argc, argv);
myWidget w (argc > 1);
w.show();
return app.exec();
} |
Partager