如何解决进程的输出文本不会附加到 QTextBrowser 对象
我仍然是 QT 的初学者。我的小程序应该将标准输出文本附加到 QTextBrowser 实例,但它没有这样做。这是我的代码:
#include <QtWidgets>
#include "mainwidget.h"
MainWidget::MainWidget(QWidget *parent):
QWidget(parent)
{
// button_,textBrowser_,process_ are declared in MainWidget Interface
button_ = new QPushButton(tr("Push Me!"));
textBrowser_ = new QTextBrowser();
process_ = new QProcess(this);
// .. some layout stuff
connect(button_,SIGNAL(released()),this,SLOT(onButtonReleased()));
connect(process_,SIGNAL(readyReadStandardOutput()),SLOT(onCaptureProcessOutput()));
}
MainWidget::~MainWidget()
{
delete button_;
delete textBrowser_;
}
void MainWidget::onButtonReleased()
{
// clear the text in the textbrowser
textBrowser_->clear();
textBrowser_->append(tr("Running command:")); // gets appended successfully when button is released
// setup the process to write to stdout and run our command
process_->setCurrentWriteChannel(QProcess::StandardOutput); // set write channel
QString program = "echo Hello";
process_->start(program); // Start the program
}
void MainWidget::onCaptureProcessOutput()
{
// Determine whether the object that sent the signal was a pointer to a process
QProcess* process = qobject_cast<QProcess*>(sender());
// of true,append the output to the textbrowser
if(process)
{
qDebug() << process->readAllStandardOutput(); // it works
QString appendText(process->readAllStandardOutput());
textBrowser_->append(appendText); // it doesn't work
}
}
问题特别在于这两行代码:
QString appendText(process->readAllStandardOutput());
textBrowser_->append(appendText);
提前致谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。