I'm having this issue executing a python3 script from QProcess. The python script is printing the time from second to second and it is working fine from command line. In Qt, the signal readyReadStandardOutput() is connected to a slot where readAllStandardOutput() is called to read the standard output from the script. The problem is that the slot is called only once! It prints the time once and then no more. The state of QProcess remains in "running" state. readyReadStandardError() and error(QProcess::ProcessError) signals are never called.
Why is the slot called only once when it has to be called every second? Thanks
Python script:
import time, threading
def foo():
print(time.ctime())
threading.Timer(1, foo).start()
foo()
Qt:
MClass::MClass(QObject* parent)
{
m_process = new QProcess(this);
connect(m_process, SIGNAL(readyReadStandardOutput()), this, SLOT(onTest()));
connect(m_process, SIGNAL(readyReadStandardError()), this, SLOT(onTestErr()));
connect(m_process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(onTestErr()));
connect(m_process, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(onTestState()));
startProcess();
}
void MClass::startProcess()
{
QString script= "../../python/test.py";
QString pythonCommand = "python3 " + script;
printf("PyCommand: %s\n", pythonCommand.toStdString().c_str());
m_process->start(pythonCommand);
// m_process->start("python3", QStringList()<<script);
}
}
void MClass::onTest()
{
qDebug()<<"------------------ON TEST";
while(m_process->canReadLine())
{
qDebug()<<m_process->readAllStandardOutput();
}
}
void MClass::onTestErr()
{
qDebug()<<"------------------ON ERR: " << m_process->errorString();
}
void MClass::onTestState()
{
qDebug()<<"------------------ STATE CHANGED: " << m_process->state();
}