通过get接口对url资源下载,并显示下载进度。
0x00 Http请求
1 | void HttpWindow::startRequest(const QUrl &requestedUrl) |
0x01 下载进度
使用以下接口获取下载进度。1
2void QNetworkReply::downloadProgress(qint64 bytesReceived,
qint64 bytesTotal);
0x02 网络验证(如有需要)
绑定authenticationRequired
信号:1
2connect(&qnam, &QNetworkAccessManager::authenticationRequired,
this, &HttpWindow::slotAuthenticationRequired);
验证动作:1
2
3
4
5
6
7
8
9
10
11
12
13oid 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安装目录找到
1
C:\Qt\{你的Qt版本}\Examples\{你的Qt版本}\network\http
相关链接
1
https://doc.qt.io/qt-5/qtnetwork-http-example.html
Qt君公众号回复『Qt示例』获取更多内容。