安庆师范学院计算机与信息学院
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+=\; }
QList
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+=\; } QList 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页 面向对象程序设计:实验报告