我们可以在主线程中下载文件,但当文件较大时,很有可能引起页面卡顿,这时候当然建议在子线程中进行文件的下载。
而在子线程中,我发现程序并不能正确执行,下面这些信号没有发出。
connect(reply, &QNetworkReply::finished, this, &download_file::on_finished);
connect(reply, &QNetworkReply::readyRead, this, &download_file::on_readyRead);
connect(reply, &QNetworkReply::downloadProgress, this, &download_file::on_downloadProgress);
参考:QNetworkAccessManager 放到线程中---finished信号不触发_qnetworkreply::finished不触发-CSDN博客解决方案:
void download_thread::run()
{
qDebug() << "download_thread:" << QThread::currentThreadId();
//下载zip到指定目录
if (d == nullptr) {
d = new download_file(url, path);
d->sendRequest();
}
this->exec();
}
this->exec();
在run()函数中添加this->exec();即可