2014-04-27 26 views
0

我想在QT Creator中創建一個簡單的GUI應用程序,它使用wget從網站下載文件。我希望能夠在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->setProcessChannelMode(QProcess::MergedChannels);

輸出:

       129K=0.09s 

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

這可能有所幫助:http://stackoverflow.com/questions/3852587/how-to-get-stdout-from-a-qprocess –

+0

@ 1.618謝謝!它確實提供了輸出,但並不完全是整個事情。我用輸出更新了問題。 –

+0

'wget'可能會根據輸出是否爲終端而改變其輸出。 (它可以通過C的'isatty'函數或類似的函數來檢測這個)。如果是這種情況,你必須做一些「創建一個僞TTY」的詭計來讓它輸出你想要的東西(我沒有細節方便;抱歉)。 –

回答

0

wget的繪製用轉義序列(可能使用詛咒)的進度條。我猜測這是阻止QProcess對象正確捕獲輸出。看起來好像沒有辦法在不禁用剩餘的詳細輸出的情況下禁用進度條。

作爲解決方法,您可以使用-o選項使wget登錄到文件。當過程完成時,讀取並顯示文件。

相關問題