Use of Qnetworkaccessmanager

Source: Internet
Author: User

Non-blocking:

   //client.h   classClient: PublicQobject {q_object Public:        ExplicitClient (ConstQString &URL); voidSendData (ConstQString &data);  PublicSlots:voidReplyfinished (qnetworkreply*reply); Private:        voidWaitforfinish (ConstQhttp &http); Qnetworkaccessmanager*M_manager; QString Murl;};//client.cpp#include<QNetworkAccessManager>#include<QNetworkReply>#include<QUrl>Client::client (ConstQString &URL) {Murl=URL; M_manager=NewQnetworkaccessmanager ( This); Connect (M_manager, SIGNAL (Finished (qnetworkreply*)), This, SLOT (replyfinished (qnetworkreply*)));}voidClient::replyfinished (qnetworkreply*reply) {//answer slot functionif(Reply && reply->error () = =qnetworkreply::noerror) {Qbytearray Data= reply->ReadAll (); Qdebug ()<<"---------------------------------\ n"<<data; } Else {        //Error} reply-close ();}voidClient::senddata (ConstQString &data)    {//Data qnetworkrequest request for sending Request.seturl (Qurl (Murl)); Set the URL request.setheader (Qnetworkrequest::contenttypeheader,"application/x-www-form-urlencoded"); Set Header information M_manager-Post (Request, Data.toutf8 ());}

of a blocked type:

QString Client::senddata (ConstQString &data) {M_manager=NewQnetworkaccessmanager ( This);    Qnetworkrequest request; Qeventloop Loop; Using Qeventloop to implement blocking qnetworkreply*reply;    Request.seturl (Qurl (Murl)); Request.setheader (Qnetworkrequest::contenttypeheader,"application/x-www-form-urlencoded"); Reply= m_manager->Post (Request, Data.toutf8 ()); Connect (M_manager, SIGNAL (Finished (qnetworkreply*), &Loop, SLOT (Quit ()));    Connect loop.exec (); Qbytearray ByteArray= reply->ReadAll (); Qdebug ()<<ByteArray;    Delete reply;    Delete M_manager; returnQString (ByteArray);}

Use of Qnetworkaccessmanager

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.