Qt: is there a way to send a signal when a QProcess writes a line to stdout

前端 未结 3 822
独厮守ぢ
独厮守ぢ 2021-01-13 17:39

I\'ve found similar questions but never the exact answer. I have Qt program that starts a QProcess and writes the output to a QTextEdit box, so far so good. But it only does

相关标签:
3条回答
  • 2021-01-13 18:21

    readLine() probably waits till the first '\n' character is read.

    readAllStandardOutput on the other hand returns all data available from the standard output of the process.

    So it seems to me that if you use readAllStandardOutput() you may get more performance.

    You have to try though.

    0 讨论(0)
  • 2021-01-13 18:30

    In an ideal world there would be some sort of signal that QProcess emits when there is a line ready to read

    Is the Qprocess signal readyReadStandardOutput() not exactly what your asking for ?

    You can connect to it then is your slot get the available data bytesAvailable() and look for the '/n' chars. Or simply readLine() while canReadLine().

    Do not forget to start your process and don't wait for it to finish. Plus set-up the ProcessChannelMode to SeparateChannels.

    EDIT

    I do have a class that read line of a QProcess like this :

    bool extProcess::logReady()
    {
        while( proc->canReadLine())
        {
            QByteArray line = proc->readLine();
            /*do some with the line like copying it's content to a QTextEdit*/
        }
    }
    

    And it work quite well. I not sure about the emit outLog() signal ! Are you try to pass a QByteArray through a signal ? The data contained in the QByteArray must be save in a buffer and you should pass the pointer to this array to anyone connected to it ...

    But again if you don't want to use extra memory and deal with the "is the data consumed ? ... ", do what you need to do with the data in your logReady() method.

    0 讨论(0)
  • 2021-01-13 18:37

    I use readAllStandardOutput() for this exact purpose and it works for me.

    However I did note that it won't recieve any standard output until the process actually flushes its output buffer ("\n" may not automagically do this, at least not in my entirely platform specific Windows experience).

    Depending on how the child process writes its output (either a C app or a C++ app), it needs to call fflush(stdout); or end lines with std::endl; respectively.

    0 讨论(0)
提交回复
热议问题