我正在尝试使用wget打印下载网站的过程输出
在一个小部件(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);

    }
}


我究竟做错了什么 ?

谢谢。

最佳答案

连接到信号readyReadStandardOutput。像这样的东西(但是未经测试):

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


在开始之前应先呼叫“课程连接”。和信号处理程序:

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


如您所见,还应调用canReadLine,因此getDownload必须可用。

关于c++ - Qt-如何将QProcess的stdout重定向到TextEdit,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34245235/

10-13 08:29