質問

I am trying to create a simple GUI application in QT Creator that uses wget to download files from websites. I want to be able to see the wget output in the GUI.

I want this output:

--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]

This is what I have so far. It downloads the file, but I don't see any output in the text browser. I am piping the QProcess output to the text browser. Commands like ls do display output.

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

}

With process->setProcessChannelMode(QProcess::MergedChannels);

Output:

                                129K=0.09s

2014-04-27 19:11:14 (129 KB/s) - ‘index.html.3’ saved [11786]
役に立ちましたか?

解決

wget draws a progress bar using escape sequences (probably using curses). I'm guessing that this is preventing the QProcess object from capturing the output correctly. It doesn't look like there's a way to disable just the progress bar without disabling the rest of the verbose output.

As a workaround, you could use the -o option to make wget log to a file. When the process completes, read and display the file.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top