C++ QProcess::readAllStandardOutput()和换行符
我正在打印C++ QProcess::readAllStandardOutput()和换行符,c++,ubuntu,qt5,qstring,qbytearray,C++,Ubuntu,Qt5,Qstring,Qbytearray,我正在打印QProcess::readAllStandardOutput()(在Ubuntu 18.04上)的输出,它在其他方面工作正常,但\n字符实际上不是换行符,以某种方式作为字符串的一部分出现: /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/
QProcess::readAllStandardOutput()
(在Ubuntu 18.04上)的输出,它在其他方面工作正常,但\n
字符实际上不是换行符,以某种方式作为字符串的一部分出现:
/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
这是在运行find/-name“*so”
命令时输出的,QProcess打印如下:
qDebug() << m_process->readAllStandardOutput();
qDebug()readAllStandardOutput();
我想这是一个编码问题。问题的原因是QDebug将显示端点和类似字符,因为您正在向它们传递QByteArray,如果您想查看所需的输出,请使用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();
}
请编辑您的问题,以显示您是如何从
QProcess
中读取/查看数据的。不知道为什么会有反对票。请记住,根据命令的不同,您将得到一个答案。另一方面,readAllStandardOutput()返回QByteArray()而不是QString(),因此您可能会得到\n
是的,但请看我自己发布的答案。正如@eyllanesc所述,qDebug
操作符我仍然想知道为什么在使用startDetached()
启动流程时没有这个问题。在这种情况下,它也会得到一个QByteArray。这取决于您调用的方式,您可以显示该情况的一部分,只需将代码中的process.start()替换为process.startDetached(),正如我看到的源代码一样,实现是不同的,可能会产生这种差异。@juzzlin在startDetached中,印象会直接发送到控制台,对qDebug()进行注释,您将看到它继续打印。
/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
...