我是 qt 开发的新手,我想将 QProcess 的输出实时传输到 textBrowser.我首先执行一个简单的 echo 命令,但没有显示程序的输出.我做错了什么????
I am a newbie in qt development and i want to transfer the output of QProcess to a textBrowser in real time. I started by executing a simple echo command,but the output of the program is not getting displayed. What am i doing wrong????
QProcess p; p.start("echo hye"); QByteArray byteArray = p.readAllStandardOutput(); QStringList strLines = QString(byteArray).split("\n"); QString line= p.readAllStandardOutput(); if(p.state()==QProcess::NotRunning) ui->textBrowser->append("not running"); foreach (QString line, strLines){ ui->textBrowser->append(line);}附言我在一台 linux 机器上.
P.S. I am on a linux machine.
我仍然无法在 textBrowser 中获得输出.
I am still not able to get the output in a textBrowser .
我更改了 Qprocess 参数并尝试了 waitForStarted() 和 waitForReadyRead() 以便进程及时启动并且结果可用.
I changed the Qprocess parameters and tried both waitForStarted() and waitForReadyRead() so that the process starts in time and the results are available.
我添加了 waitForFinished() 以便进程在超出范围时不会终止.
I added waitForFinished() so that the process does not terminate when it goes out of scope.
QProcess p; p.start("echo", QStringList() << "hye"); p.waitForStarted(); QByteArray byteArray = p.readAllStandardOutput(); QStringList strLines = QString(byteArray).split("\n"); QString line= p.readAllStandardOutput(); if(p.state()==QProcess::NotRunning) ui->textBrowser->append("not running"); ui->textBrowser->append(line); p.waitForFinished(); 推荐答案要读取标准输出,您需要在读取标准输出之前调用 waitForReadyRead() ,或者您需要连接 Qprocess 的信号 readyReadStandardOutput() 到您的插槽并从插槽读取标准输出.
to read standard output you need to either call waitForReadyRead() before reading stardard output , or you need to connect Qprocess's signal readyReadStandardOutput() to your slot and read standard output from slot.
还要确保您的 QProcess 不在堆栈中.
also make sure that your QProcess is not on stack.
我尝试以下代码工作正常.
I tried following code works fine.
MyProcess::MyProcess(QObject *parent) : QObject(parent) { QString program = "echo"; QStringList arguments; arguments << "Hello"; mProcess.start(program,arguments); connect(&mProcess,SIGNAL(readyReadStandardOutput()),this,SLOT(readyReadStandardOutput())); connect(&mProcess,SIGNAL(readyReadStandardError()),this,SLOT(readyReadStandardError())); } void MyProcess::readyReadStandardOutput(){ qDebug()<< mProcess.readAllStandardOutput(); } void MyProcess::readyReadStandardError(){ qDebug() << mProcess.readAllStandardError(); }更多推荐
在 textBrowser 中实时显示 QProcess 输出
发布评论