修正多线程写文件移动访问指针的方法名错误的问题
This commit is contained in:
parent
9278c04b93
commit
8c40d7705b
@ -13,6 +13,7 @@
|
|||||||
#include <QEventLoop>
|
#include <QEventLoop>
|
||||||
#include <QFuture>
|
#include <QFuture>
|
||||||
#include <QtConcurrent/QtConcurrent>
|
#include <QtConcurrent/QtConcurrent>
|
||||||
|
#include <QMutex>
|
||||||
|
|
||||||
Widget::Widget(QWidget *parent)
|
Widget::Widget(QWidget *parent)
|
||||||
: 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());
|
request.setRawHeader("Range", QString("bytes=%1-%2").arg(pair.first).arg(pair.second).toLocal8Bit());
|
||||||
QNetworkReply *reply = mgr.get(request);
|
QNetworkReply *reply = mgr.get(request);
|
||||||
qint64 writePos = pair.first;
|
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();
|
QByteArray data = reply->readAll();
|
||||||
lock.lock();
|
lock.lock();
|
||||||
file.peek(writePos);
|
qDebug() << "调整文件的数据指针:" << writePos;
|
||||||
|
file.seek(writePos);
|
||||||
file.write(data);
|
file.write(data);
|
||||||
bytesReceived += data.size();
|
bytesReceived += data.size();
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
@ -204,10 +208,10 @@ void Widget::on_downloadBtn_clicked()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ui->downloadBtn->setEnabled(false);
|
||||||
qint64 fileSize = getFileSize(url);
|
qint64 fileSize = getFileSize(url);
|
||||||
QString sizeText = fileSize == 0 ? "未知大小" : Utils::sizeFormat(fileSize);
|
QString sizeText = fileSize == 0 ? "未知大小" : Utils::sizeFormat(fileSize);
|
||||||
ui->filesizeLabel->setText(sizeText);
|
ui->filesizeLabel->setText(sizeText);
|
||||||
ui->downloadBtn->setEnabled(false);
|
|
||||||
|
|
||||||
int process_num = ui->threadCountSpinbox->text().toInt();
|
int process_num = ui->threadCountSpinbox->text().toInt();
|
||||||
|
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
#include <QNetworkAccessManager>
|
#include <QNetworkAccessManager>
|
||||||
#include <QMutex>
|
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
namespace Ui { class Widget; }
|
namespace Ui { class Widget; }
|
||||||
@ -32,7 +31,6 @@ private slots:
|
|||||||
private:
|
private:
|
||||||
QNetworkAccessManager *requestManager;
|
QNetworkAccessManager *requestManager;
|
||||||
const int maxThreadNum = 8;
|
const int maxThreadNum = 8;
|
||||||
QMutex lock;
|
|
||||||
|
|
||||||
Ui::Widget *ui;
|
Ui::Widget *ui;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user