2013-06-03 71 views
3

你好我编写的程序应该下载网页并保存为一个文件。它做到了,但只有部分。有没有人遇到过这样的问题?http页面没有完全下载QT

mainwindow.cpp源文件:

#include "mainwindow.h" 
#include "ui_mainwindow.h" 
#include <QDebug> 
#include <QNetworkAccessManager> 

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 
    netManager = new QNetworkAccessManager; 
    setFile("myPage"); 
} 

MainWindow::~MainWindow() 
{ 
    netManager->deleteLater(); 
    delete ui; 
} 

void MainWindow::setFile(QString fileURL) 
{ 
    QString savePath; 

    savePath = QString("D:/page.html"); 

    QNetworkRequest request; 
    request.setUrl(QUrl(fileURL)); 
    reply = netManager->get(request); 

    file = new QFile; 
    file->setFileName(savePath); 
    file->open(QIODevice::WriteOnly); 

    connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(onProgress(qint64,qint64))); 
    connect(netManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onFinished(QNetworkReply*))); 
    connect(reply, SIGNAL(readyRead()), this, SLOT(onReadyRead())); 
    connect(reply, SIGNAL(finished()), this, SLOT(onReplyFinished())); 
} 

void MainWindow::onProgress(qint64 bRead, qint64 bTotal) 
{ 
    qDebug(QString::number(bRead).toLatin1() + " - " + QString::number(bTotal).toLatin1()); 
} 

void MainWindow::onFinished(QNetworkReply *reply) 
{ 
    switch (reply->error()) 
    { 
    case QNetworkReply::NoError: 
    { 
     qDebug("File downloaded"); 
     qDebug() << file->size(); 
    }break; 
    default: 
    { 
     qDebug(reply->errorString().toLatin1()); 
    } 
    } 
    if(file->isOpen()) 
    { 
     file->close(); 
     file->deleteLater(); 
    } 
} 

void MainWindow::onReadyRead() 
{ 
    file->write(reply->readAll()); 
} 

void MainWindow::onReplyFinished() 
{ 
    if(file->isOpen()) 
    { 
     file->close(); 
     file->deleteLater(); 
    } 
} 

mainwindow.h头源文件:

#ifndef MAINWINDOW_H 
#define MAINWINDOW_H 

#include <QMainWindow> 
#include <QNetworkAccessManager> 
#include <QFile> 
#include <QNetworkReply> 
#include <QNetworkReply> 
#include <QStringList> 

namespace Ui 
{ 
    class MainWindow; 
} 

class MainWindow : public QMainWindow 
{ 
    Q_OBJECT 

public: 
    explicit MainWindow(QWidget *parent = 0); 
    ~MainWindow(); 
    void setFile(QString fileURL); 
private slots: 
    void onFinished(QNetworkReply *); 
    void onProgress(qint64 bRead, qint64 bTotal); 
    void onReadyRead(); 
    void onReplyFinished(); 
private: 
    Ui::MainWindow *ui; 
    QNetworkAccessManager *netManager; 
    QNetworkReply *reply; 
    QFile *file; 
}; 

#endif // MAINWINDOW_H 

我试图解决它自己,但多次尝试后,我失败了。我是QT的初学者,所以我在某个地方犯了个错误,我甚至都没有看到它。任何人都可以驱使我采取适当的方式解决问题吗?

+0

你使用Qt 4.x或Qt的5.x的)? –

+0

我正在使用Qt5.0 –

回答

1

你应该把这个:

connect(netManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onFinished(QNetworkReply*))); 

前:这个修改后

reply = netManager->get(request); 

工作正常;

+0

谢谢,这解决了一个问题:) –

+0

很高兴为您提供帮助;)Netmanager的和答复的** onFinished **插槽同时使用,因此您可以从其中一个插槽中退出。 – ariwez