通过get接口对url资源下载,并显示下载进度。
0x00 Http请求
void HttpWindow::startRequest(const QUrl &requestedUrl)
{
...
reply = qnam.get(QNetworkRequest(url));
connect(reply, &QNetworkReply::finished, this, &HttpWindow::httpFinished);
connect(reply, &QIODevice::readyRead, this, &HttpWindow::httpReadyRead);
ProgressDialog *progressDialog = new ProgressDialog(url, this);
progressDialog->setAttribute(Qt::WA_DeleteOnClose);
connect(progressDialog, &QProgressDialog::canceled, this, &HttpWindow::cancelDownload);
connect(reply, &QNetworkReply::downloadProgress, progressDialog, &ProgressDialog::networkReplyProgress);
connect(reply, &QNetworkReply::finished, progressDialog, &ProgressDialog::hide);
...
}0x01 下载进度
使用以下接口获取下载进度。
void QNetworkReply::downloadProgress(qint64 bytesReceived,
qint64 bytesTotal);0x02 网络验证(如有需要)
绑定authenticationRequired信号:
connect(&qnam, &QNetworkAccessManager::authenticationRequired,
this, &HttpWindow::slotAuthenticationRequired);验证动作:
oid HttpWindow::slotAuthenticationRequired(QNetworkReply *, QAuthenticator *authenticator)
{
...
// Did the URL have information? Fill the UI
// This is only relevant if the URL-supplied credentials were wrong
ui.userEdit->setText(url.userName());
ui.passwordEdit->setText(url.password());
if (authenticationDialog.exec() == QDialog::Accepted) {
authenticator->setUser(ui.userEdit->text());
authenticator->setPassword(ui.passwordEdit->text());
}
}0x03 关于更多
- 在QtCreator软件可以找到:
- 或在以下Qt安装目录找到
C:Qt{你的Qt版本}Examples{你的Qt版本}
etworkhttp- 相关链接
https://doc.qt.io/qt-5/qtnetwork-http-example.html
继续阅读与本文标签相同的文章
上一篇 :
java中参数传递方式话题终结实例
-
eclipse 插件
2026-05-16栏目: 教程
-
java8出来都5年了,内部迭代你懂了吗?
2026-05-16栏目: 教程
-
java集合|遍历HashMap的四种方法
2026-05-16栏目: 教程
-
关于编译器与解释器
2026-05-16栏目: 教程
-
IO通信模型(三)多路复用IO
2026-05-16栏目: 教程
