I'm using the Qt framework to make a GUI window, and what I do is open another application by creating a QProcess. I want to communicate with this second process, and the documentation says I can do this by writing to the process's stdin or reading from the process's stdout. I've tried many combinations and can't get it to work.
I can redirect the stdin and stdout of the new process to a file using:
process->setStandardOutputFile("d:\\outputfile.txt");
process->setStandardInputFile("d:\\inputfile.txt");
And then all cin, cout, cerr and clog commands read and write to the files. But I can't communicate directly from the Qt main window (the original application) to the new Qprocess that I create. I use:
process->write("HELLO_FROMMAINWINDOW");
and in the child application in the main loop I check stdin for any input:
fseek(stdin, 0, SEEK_END);
int num = ftell(stdin);
if (num > 0) logEntry("We have input");
The if condition is never true. Also I do std::cout in the main loop of the child process and in the Qt main window I try to read with:
QByteArray str;
str = process->readAll();
str = process->readAllStandardOutput();
But they are always empty strings. I have read the documentation at http://doc.qt.io/qt-5/qprocess.html a few times but it's possible I'm not doing things right, as in maybe I'm forgetting to set something before trying to read and write.
Here is my attempt at a verifiable example:
My main GUI window header looks like this:
//#ifndef MAINWINDOW_H
//#define MAINWINDOW_H
#pragma once
//#endif // MAINWINDOW_H
#include <QMainWindow>
class QProcess;
namespace Ui
{
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget* parent = nullptr);
~MainWindow();
Ui::MainWindow* ui;
QProcess* engineProcess;
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
}; // This is very much a stock setup with a simple main window.
Here is the main window cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QProcess>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked() // CALLED WHEN I CLICK THE START BUTTON
{// OPEN ENGINE PROCESS
engineProcess = new QProcess(this);
engineProcess->setWorkingDirectory("E:/Development/Programming/Projects/OpenGLEngine");
int w = 1600 / 2, h = 900 / 2; // JUST GIVING RESOLUTION VALUES AS ARGUMENTS
QStringList args = { "editor", std::to_string(w).c_str(), std::to_string(h).c_str() };
//engineProcess->setStandardOutputFile("d:\\outputfile.txt");
//engineProcess->setStandardInputFile("d:\\input.txt");
// THE PREVIOUS TWO CALLS WORK FINE, stdout and stdin read and write them fine.
engineProcess->start("E:/Development/Programming/Projects/OpenGLEngine/x64/Release/OpenGlEngine.exe", args);
engineProcess->write("HELLO_FROMMAINWINDOW\n"); // DOESN'T GET DETECTED
}
void MainWindow::on_pushButton_2_clicked()
{
const char* ch = "HEllo";
quint64 charsWritten = engineProcess->write(ch, 5); // charsWritten equals 5
auto result = engineProcess->write("Hello from the editor\n"); // 22 chars written, correct
engineProcess->write("HELLO_FROMMAINWINDOW\n"); // DOESN'T GET DETECTED
QByteArray str;
str = engineProcess->readAll();
str = engineProcess->readAllStandardOutput(); // EMPTY STRINGS RESULT
}