qt获取combox内容_Qt官方示例-Http

本文介绍了如何在Qt中进行Http请求,包括下载进度的跟踪,并提供了网络验证的信号绑定方法。同时,讨论了如何获取ComboBox的内容,帮助开发者更好地理解和使用Qt相关功能。
摘要由CSDN通过智能技术生成

8b62276bcd20fb6b1aee56cd0fb0c700.png
通过get接口对url资源下载,并显示下载进度。

31c1283421788319f2750db0f982fbf2.png

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

6f041821daa58c772c42439a72698102.png

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软件可以找到:

8d5f3b28ca5fbc6b1d82c9b63d4e997b.png
  • 或在以下Qt安装目录找到
C:Qt{你的Qt版本}Examples{你的Qt版本}networkhttp
  • 相关链接
https://doc.qt.io/qt-5/qtnetwork-http-example.html
  • Qt君公众号回复『Qt示例』获取更多内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值