Search code examples
c++ubuntuqt5qstringqbytearray

QProcess::readAllStandardOutput() and line feeds


I'm printing output from QProcess::readAllStandardOutput() (on Ubuntu 18.04) and it works otherwise fine, but \n characters are not actually line feeds and somehow appear literally as a part of the string:

/usr/local/lib/libpcl_search.so\n/usr/local/lib/libpcl_sample_consensus.so\n/usr/local/lib/libpcl_io.so\n/usr/local/lib/libpcl_segmentation.so\n/usr/local/lib/libpcl_common.so\n/usr/local/lib/libboost_random.so\n/usr/local/lib/libboost_math_tr1l.so

That was output when running find / -name "*so" command with QProcess printed like this:

qDebug() << m_process->readAllStandardOutput();

I guess this is an encoding issue..?


Solution

  • the problem is caused because QDebug is going to show the endlines and similar characters because you are passing them a QByteArray, if you want to see the output you want then use qPrintable:

    #include <QCoreApplication>
    #include <QProcess>
    #include <QDebug>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QProcess process;
    
        QObject::connect(&process, &QProcess::readyReadStandardOutput, [&process](){
            qDebug()<< qPrintable(process.readAllStandardOutput());
        });
        process.start("find / -name \"*so\"");
    
        return a.exec();
    }
    

    Output:

    /snap/core/4917/lib/crda/libreg.so
    /snap/core/4917/lib/i386-linux-gnu/ld-2.23.so
    /snap/core/4917/lib/i386-linux-gnu/libBrokenLocale-2.23.so
    /snap/core/4917/lib/i386-linux-gnu/libSegFault.so
    /snap/core/4917/lib/i386-linux-gnu/libanl-2.23.so
    /snap/core/4917/lib/i386-linux-gnu/libc-2.23.so
    /snap/core/4917/lib/i386-linux-gnu/libcidn-2.23.so
    ...