2015-12-12 4 views
0

Я пытаюсь распечатать выход процесса загрузки веб-сайта с помощью wget в виджетах (textEdit), но он ничего не печатает, однако в терминале он работает.Qt - Как перенаправить QProcess 'stdout в TextEdit

Пример

Команда:

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

Выход:

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

Мой код:

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); 

    } 
} 

Что утра Я делаю неправильно?

спасибо.

ответ

0

Подключиться к сигналу readyReadStandardOutput. Что-то более похожее (однако не проверено):

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

Прежде чем начать, следует вызывать соединение Of'course. И обработчик сигнала:

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

Как вы можете видеть также canReadLine следует назвать, так getDownload должны быть доступны.

+0

Привет, это по-прежнему печати ничего. – Lazai

+0

Мне удалось заставить его работать, по-видимому, проблема заключалась в том, что у меня была getDownload-> waitForFinished(); и getDownload-> close() ;. Это отменило мой вывод. Спасибо. – Lazai

1

Некоторое решение, которое я нашел. (Я новичок в Qt, но надеюсь, что это поможет):

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; 
}