Reputation: 107
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?
Upvotes: 1
Views: 2363
Reputation: 12929
The problem appears to be the way in which python
buffers stdout
(it's not line buffered). The code you posted works for me if I change the script to...
#!/usr/bin/env python2
import os
import sys
import time
while True:
print "test"
sys.stdout.flush()
time.sleep(2)
There's probably a better way to achieve the same thing without having to explicitly flush the stream constantly.
Alternatively, if you are on Linux, you could use stdbuf
to control the output buffering of the script. Change the process start command to...
process->start("stdbuf", QStringList() << "--output=L" << "/home/user/test.py");
Upvotes: 2