我需要开发一个将运行一些外部bash脚本的GUI程序。 这个脚本工作大约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); }
现在,每次脚本生成文本,你的updateText函数被调用,你将它添加到QTextEdit对象。