PDA

View Full Version : textBrowser not show anything



koccer
18th April 2013, 10:55
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() );
}

Santosh Reddy
18th April 2013, 11:19
Use QProcess::start(), instead of QProcess::execute().



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->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.

koccer
18th April 2013, 11:33
Thank you very much...