How to write multiple times to the child process from the several part of the parent code(QProcess)?

70 views Asked by At

I'm a beginner in QT, and I'm trying to send/write some data from the parent process(C++) to the child process(python) and the child process need to receive that data.

Parent code:

    if(PythonProcess.state() == QProcess::Running){
            QString data1 = "starttttttttttttttt123";
            PythonProcess.write(data1.toStdString().c_str());
            PythonProcess.write("\n\r");
            PythonProcess.closeWriteChannel();
            //PythonProcess.start();
            //PythonProcess.waitForBytesWritten();
            data1 = "newwwwwoneeee\n";
            //PythonProcess.write(data1.toStdString().c_str());
            PythonProcess.write("\n");
            //std::cout <<data1.toStdString().c_str()<<endl;
            //PythonProcess.waitForBytesWritten();
            //PythonProcess.closeWriteChannel();
        }

Child process:

    while True:
        for line in sys.stdin:
            print(line)

Problems:

When I'm using QProcess::write() to write data, I need to put QProcess::closeWriteChannel() to write the data to stdin. Without this the data isn't written to the child process. But once I closed the write channel, there's no way back to open it again without restarting the process. So if I write one time, and close the channel, the 2nd time it won't work in the same process. But I must send multiple data time to time to the child process without restarting it.

Question

How can I send the data multiple time to the child process without closing the write channel or without restarting the child process? An example will be great.

0

There are 0 answers