Chinaunix首页 | 论坛 | 博客
  • 博客访问: 530408
  • 博文数量: 71
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 159
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-13 12:37
个人简介

aaaaaaaaa

文章分类

全部博文(71)

文章存档

2013年(71)

我的朋友

分类: 嵌入式

2013-07-24 17:31:46

     看了两天的Qt网络编程,其实主要就是看了看QNetworkAccessManagerQNetworkRequestQNetworkReply这三个类的主要内容。在之前,Qt网络编程主要是使用QHttp等类,但是现在在帮助手册中这些类已经标记为过时的,所以,现在用Qt编写网络程序最好还是使用上面的三个类,之前也说过,对于一个应用程序来说,一个QNetworkAccessManager已经足够了。不知道你有没有通过名字看出这三个类的联系呢?这里再贴一下三个类的官方说明,加强印象,也有助于大家对照接下来的示例看。

QNetworkAccessManager

QNetworkRequest

QNetworkReply

Allows the application to send network requests and receive replie

Holds a request to be sent with QNetworkAccessManager

Contains the data and headers for a request sent with QNetworkAccessManager

个人感觉三者关系可简单理解如下:



           接下来就看一个示例,其实主要内容还是Nokia给的示例,但是源程序有错误之处,我这里进行修改了,这里先贴一下效果:


         再来贴一下主要的代码,代码简单,一看就懂,呵呵:

  1. #include "mainwindow.h"  
  2. #include "ui_mainwindow.h"  
  3.   
  4. #include   
  5. #include   
  6.   
  7. MainWindow::MainWindow(QWidget *parent) :  
  8.     QMainWindow(parent),  
  9.     ui(new Ui::MainWindow)  
  10. {  
  11.     ui->setupUi(this);  
  12.   
  13.     nam = new QNetworkAccessManager(this);  
  14.     QObject::connect(nam, SIGNAL(finished(QNetworkReply*)),  
  15.              this, SLOT(finishedSlot(QNetworkReply*)));  
  16. }  
  17.   
  18. MainWindow::~MainWindow()  
  19. {  
  20.     delete ui;  
  21. }  
  22.   
  23. void MainWindow::on_pushButton_clicked()  
  24. {  
  25.     QUrl url("");  
  26.     QNetworkReply* reply = nam->get(QNetworkRequest(url));  
  27.     // NOTE: Store QNetworkReply pointer (maybe into caller).  
  28.     // When this HTTP request is finished you will receive this same  
  29.     // QNetworkReply as response parameter.  
  30.     // By the QNetworkReply pointer you can identify request and response.  
  31.   
  32. }  
  33.   
  34. void MainWindow::finishedSlot(QNetworkReply *reply)  
  35. {  
  36. #if 1  
  37.      // Reading attributes of the reply  
  38.      // e.g. the HTTP status code  
  39.      QVariant statusCodeV =  
  40.      reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);  
  41.      // Or the target URL if it was a redirect:  
  42.      QVariant redirectionTargetUrl =  
  43.      reply->attribute(QNetworkRequest::RedirectionTargetAttribute);  
  44.      // see CS001432 on how to handle this  
  45.   
  46.      // no error received?  
  47.      if (reply->error() == QNetworkReply::NoError)  
  48.      {  
  49.          // read data from QNetworkReply here  
  50.   
  51.          // Example 1: Creating QImage from the reply  
  52.          //QImageReader imageReader(reply);  
  53.          //QImage pic = imageReader.read();  
  54.   
  55.          // Example 2: Reading bytes form the reply  
  56.          QByteArray bytes = reply->readAll();  // bytes  
  57.          //QString string(bytes); // string  
  58.          QString string = QString::fromUtf8(bytes);  
  59.   
  60.          ui->textBrowser->setText(string);  
  61.      }  
  62.      // Some http error received  
  63.      else  
  64.      {  
  65.          // handle errors here  
  66.      }  
  67.   
  68.      // We receive ownership of the reply object  
  69.      // and therefore need to handle deletion.  
  70.      reply->deleteLater();  
  71. #endif  
  72. }  

      好了,今天就到了!
阅读(573) | 评论(0) | 转发(0) |
0

上一篇:关于QHttp

下一篇:Qt网络编程之QNetworkReply

给主人留下些什么吧!~~