0

I'm trying to print the process output of downloading a website using wget in a widget (textEdit) , but it prints nothing , however in terminal it works.

Example

Command :

wget --no-clobber --convert-links --random-wait -r -p -E -e robots=off -U mozilla http://site/path`

Output :

Resolving ******... 54.239.26.173
Connecting to *****|54.239.26.173|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: ‘/index.html’
...

My code :

void downloadWebsite::on_pushButton_clicked()
{
    input = ui->lineEdit->text();
    if(input.isEmpty())
    QMessageBox::information(this,"Error","Not an url / webpage !");
    else{
        QProcess *getDownload = new QProcess(this);
        getDownload->setProcessChannelMode(QProcess::MergedChannels); //it prints everything , even errors
        QString command = "wget --no-clobber --convert-links --random-wait -r -p -E -e robots=off -U mozilla " + input;
        getDownload->start("sh",QStringList() << "-c" <<"cd ;"+command);


        QByteArray outputLog = getDownload->readAllStandardOutput();
        getDownload->waitForFinished();
        getDownload->close();

        QString outputToString(outputLog);
        ui->textEdit->setText(outputToString);

    }
}

What am I doing wrong ?

Thank you.

mariuss
  • 1,177
  • 2
  • 14
  • 30

2 Answers2

1

Connect to signal readyReadStandardOutput. Something more like this (however not tested):

connect(getDownload, SIGNAL(readyReadStandardOutput()), this, SLOT(readOutput()));

Of'course connect should be called before start. And signal handler:

void downloadWebsite::readOutput(){
    while(getDownload->canReadLine()){
       ui->textEdit->setText(getDownload->readLine());
    }
    // somebuffer.append(getDownload->readAllStandardOutput());
}

As you can see also canReadLine should be called, so getDownload must be available.

kwarunek
  • 12,141
  • 4
  • 43
  • 48
  • Hi , it's still printing nothing. – mariuss Dec 12 '15 at 21:53
  • I managed to make it work , apparently the issue was that I had getDownload->waitForFinished(); and getDownload->close();. Which was cancelling my output. Thank you. – mariuss Dec 12 '15 at 23:03
1

Some solution I've found. (I'm new in Qt, but hope it will help):

mainwindow.h

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

signals:
    void proccessFinished(int exitCode, QProcess::ExitStatus status);

public slots:
    void runCommand();
    void readCommand();
    void stopCommand(int exitCode, QProcess::ExitStatus exitStatus);

//private or public?
private slots:
    void error(QProcess::ProcessError error);
    void stateChanged(QProcess::ProcessState state);

private:
    Ui::MainWindow *ui;
    QProcess *cmd;
};

mainwindow.cpp

include "mainwindow.h"
include "ui_mainwindow.h"
include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    cmd = new QProcess(this);
    cmd->setProcessChannelMode(QProcess::MergedChannels);
    //button click
    connect(ui->btnRun, SIGNAL (clicked()), this, SLOT (runCommand()));
    // process has some data to read
    connect(cmd, SIGNAL (readyRead()), this, SLOT (readCommand()));
    //process finished
    connect(cmd, SIGNAL (finished(int, QProcess::ExitStatus)), this, SLOT (stopCommand(int, QProcess::ExitStatus)));
}

MainWindow::~MainWindow()
{
    delete ui;
    cmd->close(); //? how to do it right?
    delete cmd;
}


void MainWindow::runCommand()
{
    ui->output->append("Run process...");
    cmd->start("ping -n 15 google.com");
    // ??? the best way to continue here???
}

void MainWindow::readCommand(){
    ui->output->append(cmd->readAll()); //output is QTextBrowser
}
void MainWindow::stopCommand(int exitCode, QProcess::ExitStatus exitStatus){
    ui->output->append(cmd->readAll());
    ui->output->append("cmd finished");
    ui->output->append(QString::number(exitCode));
}

void MainWindow::error(QProcess::ProcessError error)
{
       qDebug() <<"Error" << error;
}

void MainWindow::stateChanged(QProcess::ProcessState state)
{
    qDebug() << "Process::stateChanged" << state;
}
Alex Granovsky
  • 2,996
  • 1
  • 15
  • 10