0

currently I start another program via QProcess and connect the read readyReadStandardOutput to a slot:

QProcess *start = new QProcess(this);
start->start(program,arguments);
connect(start, SIGNAL(readyReadStandardOutput()), log, SLOT(readyReadStandardOutput()));
connect(start, SIGNAL(readyReadStandardError()), log, SLOT(readyReadStandardError()));

log is of type myTextBrowser

class myTextBrowser : public QTextBrowser{
...
public slots:
void readyReadStandardOutput();
void readyReadStandardError();
...
}

the slots look something like this:

void myTextBrowser::readyReadStandardOutput(){
    QProcess *p = qobject_cast<QProcess*>(sender());
    QString txt (p->readAllStandardOutput());
    ...
    setText(txt);
}

The problem is, that there is a big latency. The text in my GUI only gets updated every couple of seconds, sometimes even close to a minute.

Is there a more elegant solution to forward the output to another widget?

Please note that the called program could run for a hour but the displayed output is not very long... But it is not possible to waitForFinished because I want that the GUI is still usable while the process runs.

user7431005
  • 2,471
  • 1
  • 13
  • 34
  • Maybe the latency is caused by your application itself, when it blocks the event loop and GUI is not able to process events timely? – vahancho Dec 01 '17 at 13:15
  • hmmm... how can I check this? It also happens if I do nothing with the gui. If i use the gui in the meantime and and some other signals gets emitted it works instantaneous. – user7431005 Dec 01 '17 at 13:19
  • Well, what if you call `update()` function after you set the text to the text browser with `setText(txt);`? – vahancho Dec 01 '17 at 13:24
  • 3
    How do you measure the latency? Are you sure the process writes to standard output exactly when you think it does? How do you know that? – p-a-o-l-o Dec 01 '17 at 15:03
  • You could also output your results to the debugging window using `qDebug () << txt;` to make sure it is not a GUI problem. – m7913d Dec 01 '17 at 17:18
  • 1
    I wrote an [MCVE](http://stackoverflow.com/help/mcve) about this topic as answer to [SO: Creating QT Application as GUI for existing console-based application on windows](https://stackoverflow.com/a/45209503/7478597). Code you exposed looks quite similar as what I did there. (There are some differences I wouldn't consider as relevant.) Hence, if there are unexpected delays between pieces of output, either your child process does not print text more often or something in the non-exposed code of your Qt app. is responsible. – Scheff's Cat Dec 01 '17 at 18:34

1 Answers1

0

My slot works fine and looks like this:

void EcuAppControl::readStandardOutput()
{
  QProcess *p = qobject_cast<QProcess*>(sender());
  p->setReadChannel(QProcess::StandardError);
  while(p->canReadLine())
  {
    qDebug() << p->readLine();
  }
}

Make sure you use Qt::DirectConnection. But this should be default.

Louis Kröger
  • 315
  • 2
  • 9