QT实验报告 - 图文(10)

2019-08-31 12:41

安庆师范学院计算机与信息学院

Anqing teacher school College of computer and information

m_nam=newQNetworkAccessManager(); m_reply=m_nam->get(req);

connect(m_reply,SIGNAL(error(QNetworkReply::NetworkError)), this,SLOT(onError(QNetworkReply::NetworkError)));

connect(m_nam,SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),

this,SLOT(onAuthenticationRequired(QNetworkReply*,QAuthenticator*)));

connect(m_reply,SIGNAL(finished()),this,SLOT(onFinished())); connect(m_reply,SIGNAL(readyRead()),this,SLOT(onReadyRead())); connect(m_reply,SIGNAL(downloadProgress(qint64,qint64)), this,SLOT(onDownloadProgress(qint64,qint64)));

connect(m_reply,SIGNAL(metaDataChanged()),this,SLOT(onMetaDataChanged()));

m_state->setText(requestString(&req));

m_state->append(\); }

voidWidget::onMetaDataChanged() {

m_state->append(\);

m_state->append(responseString(m_reply)); }

voidWidget::onDownloadProgress(qint64received,qint64total) {

if(total>0) {

intvalue=(received*100)/total; m_progress->setValue(value); } }

voidWidget::onReadyRead() {

QByteArraydata=m_reply->readAll();

m_state->append(QString(\).arg(data.length())); if(m_file) {

m_file->write(data); }

第43页共61页

面向对象程序设计:实验报告

安庆师范学院计算机与信息学院

Anqing teacher school College of computer and information

}

voidWidget::onError(QNetworkReply::NetworkErrorcode) {

m_reply->disconnect(this); onFinished(); }

voidWidget::onFinished() {

QStringstrState=m_reply->error()== QNetworkReply::NoError?\ :m_reply->errorString();

m_state->append(QString(\) .arg(strState));

m_state->append(\); resetState(); }

voidWidget::onAuthenticationRequired(QNetworkReply*reply, QAuthenticator*authenticator) {

//getuser&&password

m_password=QInputDialog::getText(this, \请输入密码\,\密码:\);

if(!m_password.isEmpty()) {

authenticator->setUser(

m_user.isEmpty()?\:m_user); authenticator->setPassword(m_password); } }

voidWidget::resetState() {

if(m_file) {

m_file->close(); deletem_file; m_file=0; }

if(m_reply) {

m_reply->disconnect(this);

第44页共61页

面向对象程序设计:实验报告

安庆师范学院计算机与信息学院

Anqing teacher school College of computer and information

m_reply->deleteLater(); m_reply=0; }

if(m_nam) {

m_nam->deleteLater(); m_nam=0; }

enableControls(); }

voidWidget::disableControls() {

m_userEdit->setDisabled(true);

m_passwordEdit->setDisabled(true); m_urlEdit->setDisabled(true); m_getButton->setDisabled(true); }

voidWidget::enableControls() {

m_userEdit->setEnabled(true);

m_passwordEdit->setEnabled(true); m_urlEdit->setEnabled(true); m_getButton->setEnabled(true); }

QStringWidget::requestString(QNetworkRequest*req) {

QStringjoinedHeader;

QVariantvariant=req->header(QNetworkRequest::ContentTypeHeader); if(variant.isValid()) {

joinedHeader+=\; joinedHeader+=variant.toString(); joinedHeader+=\; }

QListheaderList=req->rawHeaderList(); intcount=headerList.size(); QByteArrayvalue;

for(inti=0;i

第45页共61页

面向对象程序设计:实验报告

安庆师范学院计算机与信息学院

Anqing teacher school College of computer and information

constQByteArray&tag=headerList.at(i); value=req->rawHeader(tag);

joinedHeader+=QString(\).arg(tag.data()).arg(value.data()); }

returnjoinedHeader; }

QStringWidget::responseString(QNetworkReply*reply) {

QStringjoinedHeader;

QVariantvariant=reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);

if(variant.isValid()) {

joinedHeader=QString(\).arg(variant.toInt()); }

variant=reply->header(QNetworkRequest::ContentTypeHeader); if(variant.isValid()) {

joinedHeader+=\; joinedHeader+=variant.toString(); joinedHeader+=\; }

variant=reply->header(QNetworkRequest::ContentLengthHeader); if(variant.isValid()) {

joinedHeader+=\; joinedHeader+=variant.toString(); joinedHeader+=\; }

QListheaderList=reply->rawHeaderList(); intcount=headerList.size(); QByteArrayvalue;

for(inti=0;i

constQByteArray&tag=headerList.at(i); value=reply->rawHeader(tag);

joinedHeader+=QString(\).arg(tag.data()).arg(value.data());

第46页共61页

面向对象程序设计:实验报告

安庆师范学院计算机与信息学院

Anqing teacher school College of computer and information

}

returnjoinedHeader; }

boolWidget::openFile(boolsave,QString&errorString) {

if(m_strFilePath.isEmpty()) {

errorString=\; returnfalse; }

if(m_file) {

deletem_file; m_file=0; }

m_file=newQFile(m_strFilePath);

if(!m_file->open(save?QFile::WriteOnly:QFile::ReadOnly)) {

errorString=m_file->errorString(); deletem_file; m_file=0; returnfalse; }

returntrue; }

第47页共61页

面向对象程序设计:实验报告


QT实验报告 - 图文(10).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:劳动法期中作业答案(2)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: