Qt网络开发之QNetworkAccessManager使用要点

 要点一:对于一个应用程序,一个QNetworkAccessManager就足够了,当然使用多个QNetworkAccessManager也是没有问题的,Webkit中一个QWebpage只会使用一个QNetworkAccessManager。

    要点二:每一个回复QNetworkReply都需要删除,否则会出现内存泄露,根据Qt的帮助文档,在接收完数据的槽中使用deleteLater(),防止内存泄露。

 MyNetManager.h

 

#ifndef MYNETMANAGER_H
#define MYNETMANAGER_H
 
   
#include 
#include 
 
   
class MyNetManager : public QObject
{
    Q_OBJECT
public:
    explicit MyNetManager(QNetworkAccessManager *pManager);
    ~MyNetManager();
    bool get(QUrl url);
    bool post(QUrl url,QByteArray& bytes);
signals:
 
   
public slots:
    void downloadProgress(qint64 bytesSent, qint64 bytesTotal);
    void finished();
private:
    QNetworkAccessManager *m_pManager;
 
   
};
 
   
#endif // MYNETMANAGER_H
 
   

 

MyNetManager.cpp
 
   
#include "mynetmanager.h"
#include 
#include 
#include 
#include 
#include 
using namespace std;
 
   
MyNetManager::MyNetManager(QNetworkAccessManager *parent) :
    m_pManager(parent){
 
   
}
 
   
MyNetManager::~MyNetManager(){
 
   
}
 
   
bool MyNetManager::get(QUrl url){
    QNetworkRequest request;
    request.setUrl(url);
    QNetworkReply *reply = m_pManager->get(request);
    connect(reply,SIGNAL(finished()),this,SLOT(finished()));
    connect(reply,SIGNAL(downloadProgress(qint64,qint64)),
            this,SLOT(downloadProgress(qint64,qint64)));
    return true;
}
 
   
bool MyNetManager::post(QUrl url, QByteArray &bytes){
    QNetworkRequest request;
    request.setUrl(url);
    QNetworkReply *reply = m_pManager->post(request,bytes);
    connect(reply,SIGNAL(downloadProgress(qint64,qint64)),
            this,SLOT(downloadProgress(qint64,qint64)));
    return true;
}
 
   
void MyNetManager::finished(){
    QNetworkReply* reply = (QNetworkReply*)sender();
    reply->deleteLater();
}
 
   
void MyNetManager::downloadProgress(qint64 bytesSent, qint64 bytesTotal){
    cout<< "\ndownloadProgress done:\n";
    cout << "bytesSent: " << bytesSent
         << "  " << "bytesTocal: " << bytesTotal;
}
main.cpp
 
   
 
   
 
   
 
   
 
   
#include "mynetmanager.h"
#include 
#include 
 
   
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
 
   
    QNetworkAccessManager *netManager =new QNetworkAccessManager();
    MyNetManager manager(netManager);
    manager.get(QUrl("http://blog.csdn.net/u010002704"));
    return a.exec();
}
 
   
//以下来自Qt文档
 
   
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)),
        this, SLOT(replyFinished(QNetworkReply*)));
 
   
manager->get(QNetworkRequest(QUrl("http://qt-project.org")));
 
   
QNetworkRequest request;
request.setUrl(QUrl("http://qt-project.org"));
request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");
 
   
QNetworkReply *reply = manager->get(request);
connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
        this, SLOT(slotError(QNetworkReply::NetworkError)));
connect(reply, SIGNAL(sslErrors(QList)),
        this, SLOT(slotSslErrors(QList)));

你可能感兴趣的:(Qt网络开发之QNetworkAccessManager使用要点)