downloadProgress和完成的信号没有发出?

时间:2012-12-07 16:09:40

标签: c++ qt

我已经准备好了我认为正确的代码,以便从我的webapp处理下载,但由于某种原因,没有发出downloadProgress和完成的信号并且没有调用相关的插槽?

更新:我添加了reply = manager.get(QNetworkRequest(url);,但我不确定如何获取请求网址,因为它不是标准网址,因为PHP生成文件只是“#”? / p>

void MainWindow::unsupportedContent(QNetworkReply *reply) {

    qDebug() << "Left click - download!";
    qDebug() << "Bytes to download: " << reply->bytesAvailable();

    QString str = reply->rawHeader("Content-Disposition");

    QString end = str.mid(21);
    end.chop(1);

    qDebug() << "File name: " << end;
    qDebug() << "File type: " << reply->rawHeader("Content-Type");
    qDebug() << "File size (bytes): " << reply->bytesAvailable();
    QString defaultFileName = QFileInfo(end).fileName();
    QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), defaultFileName);
    if (fileName.isEmpty()) return;

    file = new QFile(fileName);
    if(!file->open(QIODevice::WriteOnly))
    {
        QMessageBox::information(this, "Downloader",
            tr("Unable to save the file %1: %2.")
            .arg(fileName).arg(file->errorString()));
        delete file;
        file = NULL;
        return;
    }

    downloadRequestAborted = false;

    connect(reply, SIGNAL(finished()), this, SLOT(downloadFinished()));
        connect(reply, SIGNAL(readyRead()), this, SLOT(downloadReadyRead()));
        connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
        connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload()));
        progressDialog->setLabelText(tr("Downloading %1...").arg(fileName));
        //downloadButton->setEnabled(false);
        progressDialog->exec();


    //QFile file(fileName);
    //file.open( QIODevice::WriteOnly );
    //file.write(reply->read(reply->bytesAvailable()));
    //file.close();
}

void MainWindow::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{
    qDebug() << bytesReceived << bytesTotal;
    if(downloadRequestAborted)
        return;
    progressDialog->setMaximum(bytesTotal);
    progressDialog->setValue(bytesReceived);
}

void MainWindow::downloadReadyRead()
{
    if(file)
        file->write(reply->read(reply->bytesAvailable()));
}

void MainWindow::downloadFinished()
{
    qDebug() << "Download finished!";
    if(downloadRequestAborted)
    {
        if(file)
        {
            file->close();
            file->remove();
            delete file;
            file = NULL;
        }
        reply->deleteLater();
        progressDialog->hide();
        //downloadButton->setEnabled(true);
        return;
    }

    downloadReadyRead();
    progressDialog->hide();
    //downloadButton->setEnabled(true);
    file->flush();
    file->close();

    if(reply->error())
    {
        //Download failed
        QMessageBox::information(this, "Download failed", tr("Failed: %1").arg(reply->errorString()));
    }

    reply->deleteLater();
    reply = NULL;
    delete file;
    file = NULL;
}

void MainWindow::cancelDownload()
{
    downloadRequestAborted = true;
    reply->abort();
    progressDialog->hide();
    //downloadButton->setEnabled(true);
}

0 个答案:

没有答案