我需要开发一个gui程序,它将运行一些外部bash脚本.这个脚本工作大约30-40分钟,我希望在我的应用程序中实时查看系统输出.我怎么能提供这个?我应该使用QTextStream吗?请举个例子.谢谢.
如果通过QProcess启动脚本,则可以通过连接到readyRead信号来获取输出.然后,只需调用任何读取函数来获取数据,然后将其显示在您想要的任何类型的小部件上,例如QTextEdit,它具有用于添加文本的追加功能.
像这样: -
// Assuming QTextEdit textEdit has been created and this is in a class
// with a slot called updateText()
QProcess* proc = new QProcess;
connect(proc, SIGNAL(readyRead()), this, SLOT(updateText()));
proc->start("pathToScript");
...
// updateText in a class that stored a pointer to the QProcess, proc
void ClassName::updateText()
{
QString appendText(proc->readAll());
textEdit.append(appendText);
}
Run Code Online (Sandbox Code Playgroud)
现在,每次脚本生成文本时,都会调用updateText函数,并将其添加到QTextEdit对象中.