在 textBrowser 中实时显示 QProcess 输出

编程入门 行业动态 更新时间:2024-10-19 22:16:06
本文介绍了在 textBrowser 中实时显示 QProcess 输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是 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 输出

本文发布于:2023-11-02 14:34:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552663.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实时   textBrowser   QProcess

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!