I'm trying to get a Qt application to communicate with a python program. The most logical solution seemed to be to run a QProcess in the Qt app containing the Python code. I want to send commands using std input and if applicable read via the std output.
However, even this simple example doesn't seem to work. These two python snippets:
import os
import time
while True:
print "test"
time.sleep(2)
Together with the simple Qt code:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
process = new QProcess(this);
process->start("/home/user/test.py");
connect(process, SIGNAL(stateChanged(QProcess::ProcessState)), SLOT(printProcessStatus()));
connect(process, SIGNAL(error(QProcess::ProcessError)), SLOT(printProcessError()));
connect(process, SIGNAL(readyRead()), SLOT(printProcessOutput()));
}
void MainWindow::printProcessStatus()
{
qDebug() << process->state();
}
void MainWindow::printProcessError()
{
qDebug() << process->errorString();
}
void MainWindow::printProcessOutput()
{
qDebug() << process->readAll();
}
Doesn't print anything. It does say that the process is "QProcess::ProcessState(Running)", but I can't seem to get the printed output from python into Qt. Similarly I've tried to use the QProcess::write() function to write to a python process but that doesn't work either.
Is this not the intended way to work with QProcess? Is there a better way to do communication between a Qt app and a (child) python program?