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; };