From 8c40d7705b0ac752605bc59091175ff5477f69dd Mon Sep 17 00:00:00 2001 From: metanoia1989 Date: Sun, 25 Oct 2020 22:19:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E5=A4=9A=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E5=86=99=E6=96=87=E4=BB=B6=E7=A7=BB=E5=8A=A8=E8=AE=BF=E9=97=AE?= =?UTF-8?q?=E6=8C=87=E9=92=88=E7=9A=84=E6=96=B9=E6=B3=95=E5=90=8D=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MultiplethreadDownload/widget.cpp | 10 +++++++--- MultiplethreadDownload/widget.h | 2 -- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/MultiplethreadDownload/widget.cpp b/MultiplethreadDownload/widget.cpp index a7b7221..4622595 100644 --- a/MultiplethreadDownload/widget.cpp +++ b/MultiplethreadDownload/widget.cpp @@ -13,6 +13,7 @@ #include #include #include +#include Widget::Widget(QWidget *parent) : QWidget(parent) @@ -156,10 +157,13 @@ void Widget::multiDownload(const QString &url, qint64 fileSize, const QString &f request.setRawHeader("Range", QString("bytes=%1-%2").arg(pair.first).arg(pair.second).toLocal8Bit()); QNetworkReply *reply = mgr.get(request); qint64 writePos = pair.first; - connect(reply, &QNetworkReply::readyRead, [&, reply, this](){ + QMutex lock; + qDebug() << "开始下载数据:" << QString(" %1~%2 ").arg(pair.first).arg(pair.second); + connect(reply, &QNetworkReply::readyRead, [&lock, &writePos, &file, &bytesReceived, reply](){ QByteArray data = reply->readAll(); lock.lock(); - file.peek(writePos); + qDebug() << "调整文件的数据指针:" << writePos; + file.seek(writePos); file.write(data); bytesReceived += data.size(); lock.unlock(); @@ -204,10 +208,10 @@ void Widget::on_downloadBtn_clicked() } } + ui->downloadBtn->setEnabled(false); qint64 fileSize = getFileSize(url); QString sizeText = fileSize == 0 ? "未知大小" : Utils::sizeFormat(fileSize); ui->filesizeLabel->setText(sizeText); - ui->downloadBtn->setEnabled(false); int process_num = ui->threadCountSpinbox->text().toInt(); diff --git a/MultiplethreadDownload/widget.h b/MultiplethreadDownload/widget.h index 4ab03f6..164947b 100644 --- a/MultiplethreadDownload/widget.h +++ b/MultiplethreadDownload/widget.h @@ -3,7 +3,6 @@ #include #include -#include QT_BEGIN_NAMESPACE namespace Ui { class Widget; } @@ -32,7 +31,6 @@ private slots: private: QNetworkAccessManager *requestManager; const int maxThreadNum = 8; - QMutex lock; Ui::Widget *ui; };