QProcess::readyReadStandardError() signal not emit until the process closed?

689 Views Asked by At

I'm trying to redirect other process's stdout and stderr to my Qt GUI app. But I notice that QProcess::readyReadStandardError() signal only emitted (once) when the other process is closing.

Is there something wrong in my code?

void MainWindow::slotPreview()
{
    // ... other stuff...

    m_process = new QProcess(this);
    m_process->setWorkingDirectory(qApp->applicationDirPath());
    connect(m_process, SIGNAL(readyReadStandardOutput()), this, SLOT(slotPreviewProcessHasStdOut()));
    connect(m_process, SIGNAL(readyReadStandardError()), this, SLOT(slotPreviewProcessHasStdErr()));
    connect(m_process, SIGNAL(finished(int)), this, SLOT(slotPreviewProcessFinished()));
    m_process->start("executor.exe");
}

void MainWindow::slotPreviewProcessHasStdErr()
{
    ui->output->appendText(m_process->readAllStandardError());
}

void MainWindow::slotPreviewProcessHasStdOut()
{
    ui->output->appendText(m_process->readAllStandardOutput());
}

void MainWindow::slotPreviewProcessFinished()
{
    m_process->deleteLater();
    m_process = Q_NULLPTR;
}

In executor.exe app, I use google glog to log to stderr as below:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    google::InitGoogleLogging(argv[0]);
    google::LogToStderr();
    {
    LOG(INFO) << "log started...";
    qDebug() << "qDebug started...";
    std::cout << "std::cout started..." << std::endl;
    std::cerr << "std::cerr started..." << std::endl;
    }
    MainWindow w;
    w.show();
    LOG(INFO) << "MainWindow displayed";
    return a.exec();
}

==============Update 1======================

Attach the output:

// before process closing:

std::cout started...
I0906 12:26:03.022691  3856 main.cpp:49] log started...
std::cerr started...

// when process is closing:
I0906 12:26:03.106772  3856 main.cpp:65] MainWindow displayed

It seems that:

  1. statement LOG(INFO) << "log started..."; is not flushed until statement std::cerr << "std::cerr started..." << std::endl; is about to execute.
  2. output of statement qDebug() << "qDebug started..."; is missing,

I have tried add google::FlushLogFiles(google::GLOG_INFO); after each LOG(INFO), did not fix the problem;

I have tried remove all other print statement except the LOG(INFO) one, did not fix the problem, too.

0

There are 0 best solutions below