textBrowser not show anything
Why codes are not running. There is not any affect on textBrowser...?
#include <QProcess>
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_exit_clicked()
{
this->close();
}
void MainWindow::on_button_clicked()
{
QProcess *p = new QProcess( this );
if (p)
{
p->setEnvironment( QProcess::systemEnvironment() );
p->setProcessChannelMode( QProcess::MergedChannels );
connect( p, SIGNAL(readyReadStandardOutput()), this, SLOT(ReadOut()) );
connect( p, SIGNAL(readyReadStandardError()), this, SLOT(ReadErr()) );
p->execute("sh /home/user/Desktop/file.sh");
p->waitForStarted();
}
}
void MainWindow::ReadOut()
{
QProcess *p = dynamic_cast<QProcess *>( sender() );
if (p)
ui->textBrowser->append( p->readAllStandardOutput() );
}
void MainWindow::ReadErr()
{
QProcess *p = dynamic_cast<QProcess *>( sender() );
if (p)
ui->textBrowser->append( p->readAllStandardError() );
}
Re: textBrowser not show anything
Use QProcess::start(), instead of QProcess::execute().
Code:
void MainWindow::on_button_clicked()
{
if (p)
{
p
->setEnvironment
( QProcess::systemEnvironment() );
p
->setProcessChannelMode
( QProcess::MergedChannels );
connect( p, SIGNAL(readyReadStandardOutput()), this, SLOT(ReadOut()) );
connect( p, SIGNAL(readyReadStandardError()), this, SLOT(ReadErr()) );
//p->execute("sh /home/user/Desktop/file.sh");
p->start("sh /home/user/Desktop/file.sh");
p->waitForStarted();
}
}
QProcess::execute(), is a static call and will start another new process (which is not p), you actualy need to connect to the signals of the new process created in side execute() function, but you cannot access it.
Re: textBrowser not show anything