c++ - Qt - 如何将 QProcess 的标准输出重定向到 TextEdit

标签 c++ qt stdout qprocess

我正在尝试打印使用 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()));

当然应该在开始之前调用connect。和信号处理程序:

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

您还可以看到 canReadLine 应该被调用,所以 getDownload 必须可用。

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

相关文章:

qt - 如果我想使用 Qt 中的对话框编辑项目,我应该使用委托(delegate)类吗?

c - 我想用 FILE 的自定义实现在 ANSI C 中模拟一个 FILE

c++ - QT:发出信号时如何创建新按钮

c++ - 总结一串数字输入的最佳方法

python - Grep python 输出流(尝试打印和标准输出)

c++ - 在派生类中可见的私有(private) typedef

c++ - Qt - 找不到合适的发件人()

c++ - 如何通过内容正则表达式匹配过滤 pcap(例如 tcpdump)文件?

c# - 如何发送ctrl+z

c++ - 在不同的线程中执行每个QTModbus响应