Results 1 to 1 of 1

Thread: QNetworkAdapter::Post working in Main Application but Not from a Library

  1. #1
    Join Date
    Aug 2012
    Location
    India
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkAdapter::Post working in Main Application but Not from a Library

    I want to http post a file to a server though a library used by my application. As the post failed (WireShark shows data not sent from Client).

    So, I shifted the code to a function in the Main application...and viola it works... (Data received by Server)

    What am I missing, QEventLoop? If yes, how to use it. Tried, but am lost..

    Here is the code (used in the Main Application):
    Qt Code:
    1. //loginscreenmain.h
    2. class loginscreenmain : public QMainWindow
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7. explicit loginscreenmain(QWidget *parent = 0);
    8. ~loginscreenmain();
    9. QNetworkAccessManager* m_pNAM;
    10. QNetworkReply *m_Reply;
    11. QNetworkRequest m_Request;
    12.  
    13. void SendPacket();
    14.  
    15. private slots:
    16. void on_pushButton_2_clicked();
    17. void on_pushButton_clicked();
    18. void on_pushButton_2_pressed();
    19. void on_pushButton_pressed();
    20.  
    21. private:
    22. Ui::loginscreenmain *ui;
    23. };
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. void loginscreenmain::SendPacket()
    2. {
    3. CRequestGenerator *oReqGen = new CRequestGenerator(); //Mediator class between Google Protobuf classes and Client Code
    4.  
    5. CMyRequest *oReq = oReqGen->createMyRequest(); //CMyRequest is Google Protobuf generated class
    6.  
    7. m_pNAM = NULL;
    8. m_pNAM = new QNetworkAccessManager();
    9.  
    10. int checksize = oReq->ByteSize();
    11.  
    12. std::string outputstring;
    13. outputstring = oReq->SerializeAsString(); //Google Protobuf API
    14. checksize = outputstring.size();
    15.  
    16. //An UTF8 encoded QByteArray
    17. QByteArray byteArray(outputstring.c_str(), outputstring.length());
    18. checksize = byteArray.size();
    19.  
    20. QUrl oUrl = QUrl(QString("http://blahblahblah")); //URI given here...Tested to Work
    21.  
    22. m_Request.setUrl(oUrl);
    23. m_Request.setRawHeader("function", "PerformAddition" ); //Protobuf Specific
    24.  
    25. if(m_pNAM)
    26. {
    27. m_Reply = m_pNAM->post(m_Request,byteArray);
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 


    Added after 27 minutes:


    Found the solution: QEventLoop was needed to make the post synchronous.

    Qt Code:
    1. if(m_pNAM)
    2. {
    3. m_pReply = m_pNAM->post(m_pRequest,m_byteArray);
    4. QEventLoop eventLoop;
    5. QObject::connect(m_pReply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
    6. eventLoop.exec();
    7. std::cout << "finished" << std::endl; //request finished here
    8. }
    To copy to clipboard, switch view to plain text mode 

    Can also refer: http://www.qtcentre.org/threads/3511...and-QEventLoop
    Last edited by pp1010; 6th May 2013 at 07:49.

Similar Threads

  1. Replies: 5
    Last Post: 8th February 2012, 09:56
  2. HTTP request POST in QT application
    By abdul_moiz in forum Newbie
    Replies: 5
    Last Post: 24th June 2011, 03:48
  3. main window close not working
    By hvranic in forum Newbie
    Replies: 2
    Last Post: 17th June 2010, 21:12
  4. Replies: 0
    Last Post: 21st December 2009, 05:04
  5. How to post a key event to another application
    By mitskits in forum Qt Programming
    Replies: 1
    Last Post: 23rd June 2006, 13:09

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.