Results 1 to 2 of 2

Thread: Access network using QNetworkaccessmanger doesn’t work on seperate class

  1. #1
    Join Date
    Jan 2011
    Location
    Nagercoil,Tamilnadu,India
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    4

    Default Access network using QNetworkaccessmanger doesn’t work on seperate class

    Hi, i have created a c++ header file for network access and i am able access the network when i create instance in main.cpp, but it is not working(only network access like get,post… remains works fine) when i call it on a separate class.

    Sync_Network.h

    Qt Code:
    1. #include <QNetworkAccessManager>
    2. #include <QNetworkReply>
    3. #include <QNetworkRequest>
    4. #include <QDebug>
    5. #include <QUrl>
    6. class Sync_Network:public QNetworkAccessManager{
    7. Q_OBJECT
    8. public:
    9. Sync_Network(QString url,QObject *parent=0):QNetworkAccessManager(parent),host(url){
    10. connect(this,SIGNAL(finished(QNetworkReply*)),this,SLOT(showReply(QNetworkReply*)));
    11. }
    12. virtual ~Sync_Network(){}
    13. bool Sync_Get(const QString path){
    14. get(QNetworkRequest(QUrl(host+""+path)));
    15. return status;
    16. }
    17. bool Sync_Post(QString params,QString path=""){
    18. QByteArray data;
    19. data.append(params);
    20. post(QNetworkRequest(QUrl(host+""+path)),data);
    21. return status;
    22. }
    23. protected slots:
    24. virtual void showReply(QNetworkReply *reply){
    25. if(!reply->error()) qDebug()<<reply->readAll();
    26. else{
    27. status=false;
    28. qDebug()<<reply->errorString();
    29. }
    30. }
    31. private:
    32. bool status;
    33. QString host;
    34. };
    To copy to clipboard, switch view to plain text mode 

    tweet.cpp

    Qt Code:
    1. #include "Sync_Network.h"
    2.  
    3. Tweet::Tweet(QWidget *parent) :
    4. QMainWindow(parent),
    5. ui(new Ui::Tweet)
    6. {
    7. ui->setupUi(this);
    8. Sync_Network s("http://www.capeconsultancy.com");
    9. s.Sync_Get("/aboutus");
    10. s.Sync_Post("<tweet><username>dinesh</username><passowrd>123456</password></tweet>");
    11. }
    To copy to clipboard, switch view to plain text mode 

    Please help me. Thanks in advance
    M Dineshkumar,
    Project Trainee,
    Cape Consultancy Services
    (http://www.capeconsultancy.com)
    India.

  2. #2
    Join Date
    Oct 2010
    Posts
    55
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    1
    Thanked 11 Times in 10 Posts
    Wiki edits
    9

    Default Re: Access network using QNetworkaccessmanger doesn’t work on seperate class

    I think it is because 's' falls out of scope and is destroyed.. change your code to:

    Qt Code:
    1. {
    2. ui->setupUi(this);
    3. Sync_Network* s = new Sync_Network("http://www.capeconsultancy.com", this);
    4. s->Sync_Get("/aboutus");
    5. s->Sync_Post("<tweet><username>dinesh</username><passowrd>123456</password></tweet>");
    6. }
    To copy to clipboard, switch view to plain text mode 

    Edit: That is, it is not synchronous at all, so it's not really a good solution, but it solves the problem of the signal not being emitted.

    What you want is probably Sync_Get to wait before returning:

    Qt Code:
    1. get(QNetworkRequest(QUrl(host+""+path)));
    2. loop.exec();
    To copy to clipboard, switch view to plain text mode 

    then in showReply(QNetworkReply *reply), you exit the event loop:

    Qt Code:
    1. loop.exit();
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. private:
    2. bool status;
    3. QString host;
    4. QEventLoop loop;
    To copy to clipboard, switch view to plain text mode 

    .. then you can create your Sync_Network on the stack without it being destroyed before the reply is received.
    Last edited by helloworld; 4th February 2011 at 10:13.

Similar Threads

  1. Qt Designer Visual Studio Plugin: Qt Designer doesn’t work
    By worstnbro in forum Qt Tools
    Replies: 3
    Last Post: 4th April 2012, 14:36
  2. QThread and QNetworkAccessManger
    By dpatel in forum Qt Programming
    Replies: 1
    Last Post: 24th October 2010, 08:07
  3. Replies: 4
    Last Post: 29th May 2010, 13:56
  4. access the network to read files
    By jaca in forum Newbie
    Replies: 3
    Last Post: 2nd March 2010, 14:00
  5. Replies: 5
    Last Post: 14th July 2006, 23:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.