简体   繁体   English

将QProcess输出发送到textedit的最佳方法

[英]best way to send QProcess output to textedit

currently I start another program via QProcess and connect the read readyReadStandardOutput to a slot: 当前,我通过QProcess启动另一个程序,并将读取的readyReadStandardOutput连接到插槽:

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 日志的类型为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. 我的GUI中的文本每隔几秒钟才更新一次,有时甚至接近一分钟。

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. 请注意,被调用的程序可能会运行一个小时,但显示的输出不是很长。。。但是因为我希望在进程运行时GUI仍然可用,所以无法使用waitForFinished

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 . 确保使用Qt::DirectConnection But this should be default. 但这应该是默认的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM