【问题标题】:Display wget console output in Gui在 Gui 中显示 wget 控制台输出
【发布时间】:2014-04-27 21:00:50
【问题描述】:

我正在尝试在 QT Creator 中创建一个简单的 GUI 应用程序,它使用 wget 从网站下载文件。我希望能够在 GUI 中看到 wget 输出。

我想要这个输出:

--2014-04-27 13:58:58--  http://google.com/
Resolving google.com (google.com)... 74.125.224.192, 74.125.224.193, 74.125.224.206, ...
Connecting to google.com (google.com)|74.125.224.192|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://www.google.com/ [following]
--2014-04-27 13:58:58--  http://www.google.com/
Resolving www.google.com (www.google.com)... 74.125.224.210, 74.125.224.209, 74.125.224.208, ...
Connecting to www.google.com (www.google.com)|74.125.224.210|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: ‘index.html’

    [ <=>                                   ] 11,802      --.-K/s   in 0.005s  

2014-04-27 13:58:58 (2.16 MB/s) - ‘index.html’ saved [11802]

这是我目前所拥有的。它下载文件,但我在文本浏览器中看不到任何输出。我将 QProcess 输出传送到文本浏览器。像ls 这样的命令会显示输出。

    #include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QProcess>
#include <stdlib.h>
#include <string>
using namespace std;




MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    process = new QProcess(this);
    //connect (process, SIGNAL(readyReadStandardOutput()), this, SLOT(printOutput()));

}



MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    ui->label->setText("Pressed!");



}
void MainWindow::printOutput()
{
    ui->display->setPlainText(process->readAllStandardOutput());
}


void MainWindow::on_lineEdit_returnPressed()
{    connect (process, SIGNAL(readyReadStandardOutput()), this, SLOT(printOutput()));


    string result = "wget ";
    result += ui->lineEdit->text().toStdString();

    ui->label->setText("Fetching...");
    QString lawl = QString::fromStdString(result);

    options.clear();
#if defined(Q_OS_LINUX)
    options << "-c" << lawl;
    process->start("/bin/sh", options);
#elif defined(Q_OS_WIN)
    process->start(ui->lineEdit->text(), options);
#endif
    process->waitForFinished();

}

process-&gt;setProcessChannelMode(QProcess::MergedChannels);

输出:

                                129K=0.09s

2014-04-27 19:11:14 (129 KB/s) - ‘index.html.3’ saved [11786]

【问题讨论】:

  • 这可能会有所帮助:stackoverflow.com/questions/3852587/…
  • @1.618 谢谢!它确实提供了输出,但不是全部。我用输出更新了问题。
  • wget 可能会根据其输出是否为终端来更改其输出。 (它可以通过 C 的 isatty 函数或等效函数检测到这一点。)如果是这种情况,你必须做一些“创建一个伪 TTY”的技巧来让它输出你想要的东西(我手边没有细节; 抱歉)。
  • 你可以做的另一件事,因为你是从程序的角度来解决这个问题的,所以改为使用libcurl

标签: c++ qt-creator wget


【解决方案1】:

wget 使用转义序列(可能使用curses)绘制进度条。我猜这会阻止 QProcess 对象正确捕获输出。似乎没有办法只禁用进度条而不禁用其余的详细输出。

作为一种解决方法,您可以使用 -o 选项将 wget 记录到文件中。该过程完成后,读取并显示该文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-12
    • 2012-08-22
    • 2020-01-12
    • 1970-01-01
    相关资源
    最近更新 更多