qt 之http學習

在Qt網絡編程中,須要用到協議,即HTTP。它是超文本傳輸協議,它是一種文件傳輸協議。html

新建工程名爲「http」,而後選中QtNetwork模塊,最後Base class選擇QWidget。注意:若是新建工程時沒有添加QtNetwork模塊,那麼就要手動在工程文件.pro中添加代碼linux

  1. QT += network  

代表咱們使用了網絡模塊。編程

2.咱們在widget.ui文件中添加一個 Text Browser ,以下圖。網絡

Qt網絡之HTTP編程

 

實現的代碼以下:ide

widget.h文件:函數

#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QNetworkAccessManager> #include <qtextcodec.h> #include <QNetworkReply>
namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); private: Ui::Widget *ui; QNetworkAccessManager* manager; private slots: void replyFinished(QNetworkReply *); }; #endif // WIDGET_H

widget.cpp文件:ui

#include "widget.h" #include "ui_widget.h" Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); manager = new QNetworkAccessManager(this); connect(manager,SIGNAL(finished(QNetworkReply*)),  //關聯信號和槽
                this,SLOT(replyFinished(QNetworkReply*))); manager->get(QNetworkRequest(QUrl("http://www.baidu.com"))); //發送請求
} Widget::~Widget() { delete ui; } void Widget::replyFinished(QNetworkReply *reply)  //當回覆結束後
{ QTextCodec *codec = QTextCodec::codecForName("utf8"); //使用utf8編碼,這樣才能夠顯示中文
    QString all = codec->toUnicode(reply->readAll()); ui->textBrowser->setText(all); reply->deleteLater();   //最後要釋放reply對象
}

 

代碼分析。this

上面實現了最簡單的應用HTTP協議下載網頁的程序。QNetworkAccessManager類用於發送網絡請求和接受回覆,具體的,它是用QNetworkRequest類來管理請求,QNetworkReply類進行接收回復,並對數據進行處理。編碼

在上面的代碼中,咱們使用了下面的代碼來發送請求:url

  1. manager->get(QNetworkRequest(QUrl(「http://www.yafeilinux.com」))); 

它返回一個QNetworkReply對象,這個下面再講。咱們只需知道只要發送請求成功,它就會下載數據。而當數據下載完成後,manager會發出finished()信號,咱們對它進行了關聯:

  1. connect(manager,SIGNAL(finished(QNetworkReply*)),  
  2.             this,SLOT(replyFinished(QNetworkReply*))); 

也就是說,當下載數據結束時,就會執行replyFinished()函數。在這個函數中咱們對接收的數據進行處理:

  1. QTextCodec *codec = QTextCodec::codecForName(「utf8″); 
  2. QString all = codec->toUnicode(reply->readAll()); 
  3. ui->textBrowser->setText(all); 

這裏,爲了能顯示下載的網頁中的中文,咱們使用了QTextCodec 類對象,應用utf8編碼。

使用reply->readAll()函數就能夠將下載的全部數據讀出。而後,咱們在textBrowser中將數據顯示出來。當reply對象已經完成了它的功能時,咱們須要將它釋放,就是最後一條代碼:

  1. reply->deleteLater(); 
 
2、功能擴展
開始咱們先讓進度條隱藏。當咱們在Line 
Edit中輸入下載地址,點擊下載按鈕後,咱們應用輸入的下載地址,得到文件名,在磁盤上新建一個文件,用於保存下載的數據,而後進行連接,並顯示進度
條。在下載過程當中,咱們將每次得到的數據都寫入文件中,並更新進度條,在接收完文件後,咱們從新隱藏進度條,並作一些清理工做。

 

 
 
經過上面的例子能夠看到,Qt中編寫基於HTTP協議的程序是十分簡單的,只有十幾行代碼。不過,通常咱們下載文件都想要看到下載進度。下面咱們就更改上面的程序,讓它能夠下載任意的文件,而且顯示下載進度。

#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QNetworkAccessManager> #include <qtextcodec.h> #include <QNetworkReply> #include <QFile> #include <QFileInfo> #include <QDebug>
namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); void startRequest(QUrl url); //請求連接
protected: //void changeEvent(QEvent *e);

private: Ui::Widget *ui; QNetworkAccessManager* manager; QNetworkReply *reply; QUrl url; //存儲網絡地址
    QFile *file;//存儲文件
private slots: void on_download_clicked(); void httpFinshed(); void httpReadyRead(); void updataDataReadProcess(qint64,qint64 );//更新進度條
}; #endif // WIDGET_H

3.widget.cpp文件

#include "widget.h" #include "ui_widget.h" Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); manager = new QNetworkAccessManager(this); ui->progressBar->hide(); } Widget::~Widget() { delete ui; } void Widget::on_download_clicked() { url=ui->lineEdit->text(); //獲取在界面輸入的url地址
 QFileInfo info(url.path()); QString filename(info.fileName()); //獲取文件名
    if(filename.isEmpty()) filename="index.html"; file=new QFile(filename); if(!file->open(QIODevice::WriteOnly)) { //qDebug<<"file open errror";
 delete file; file=0; return; } startRequest(url);//進行連接請求
    ui->progressBar->setValue(0);//進度條的值設爲零
    ui->progressBar->show(); //顯示進度條
} void Widget::startRequest(QUrl url)  //連接請求
{ reply = manager->get(QNetworkRequest(url)); //下面關聯信號和槽
    connect(reply,SIGNAL(finished()),this,SLOT(httpFinished())); //下載完成後
    connect(reply,SIGNAL(readyRead()),this,SLOT(httpReadyRead())); //有可用數據時
 connect(reply,SIGNAL(downloadProgress(qint64,qint64)), this,SLOT(updateDataReadProgress(qint64,qint64))); //更新進度條
} void Widget::httpReadyRead()   //有可用數據
{ if (file) file->write(reply->readAll());  //若是文件存在,則寫入文件
} void Widget::updataDataReadProcess(qint64 bytesRead, qint64 totalBytes) { ui->progressBar->setMaximum(totalBytes); //最大值
    ui->progressBar->setValue(bytesRead);  //當前值
} void Widget::httpFinshed()  //完成下載
{ ui->progressBar->hide(); file->flush(); file->close(); reply->deleteLater(); reply = 0; delete file; file = 0; }

咱們HTTP應用的內容就講到這裏,能夠看到它是很容易的,也不須要你瞭解太多的HTTP的原理知識。關於相關的類的其餘使用,你能夠查看其幫助

相關文章
相關標籤/搜索