February 21, 2012

maybnxtseasn maybnxtseasn
Lab Rat
21 posts

Connection/Slot question

 

I have the following Class in creation that looks like so

  1. class CQueryHandler : public QObject
  2. {
  3.  Q_OBJECT
  4.  
  5. public:
  6.  CQueryHandler(QObject* pParent = 0);
  7.  bool Initialize();
  8.  ~CQueryHandler();
  9.  
  10. public:
  11.  void SendQuery(const QString& strQuery);
  12.  
  13. protected slots:
  14.  void QueryFinished(QNetworkReply* pNetReply);
  15.  
  16. private:
  17.  QNetworkAccessManager* m_pNetworkManager;
  18.         QByteArray                      m_ByteArray;
  19.  
  20. } g_QueryHandler;

  1. CQueryHandler::CQueryHandler(QObject* pParent)
  2.  : QObject(pParent), m_pNetworkManager(NULL)
  3. {
  4.  
  5. }
  6.  
  7. bool CQueryHandler::Initialize()
  8. {
  9.  // Initialized Network Manager
  10.  m_pNetworkManager = new QNetworkAccessManager(this);
  11.  if (m_pNetworkManager == NULL) {
  12.   return false;
  13.  }
  14.  
  15.  // Setup Connections
  16.  connect(m_pNetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(QueryFinished(QNetworkReply*)));
  17. }
  18.  
  19.  
  20. void CQueryHandler::QueryFinished(QNetworkReply* pNetReply)
  21. {
  22.        // TODO: Error checking
  23.  
  24.        // Perform Parsing and extra things from returned query
  25.  
  26.        // Extract Data from reply to a QByteArray  Object
  27.         m_ByteArray = pNetReply->readAll();
  28. }

This is just a rough outline of what i have. Pretty much i have CRequestHandler…Which will request my webserver for a particular Query and upon completion and retrieval of the information will perform some extra processing and parsing and save the information in my BYTEARRAY member.

If my program is using the above global object( g_QueryHandler ), and i have a simple dialog with a button the user can click, i understand how to send the QueryRequest as shown below

  1. void Dialog::Clicked()
  2. {
  3.      g_QueryHandler.SendQuery("somequery to send to webserver");
  4. }

But what im unsure of is how to INFORM my Dialog:: that the request has finished and the above Dialog:: object can check the Query information returned from the request inside of g_QueryHandler.m_ByteArray member variable?

3 replies

February 22, 2012

p-himik p-himik
Lab Rat
255 posts

Add this to CQueryHandler’s definition:

  1. signals:
  2.     void queryFinished();

Add this to Dialog’s definition:

  1. public slots:
  2.     void queryHandlerFinished();

Add this to Dialog:Dialog():

  1. connect( &g_QueryHandler, SIGNAL( queryFinished() ), SLOT( queryHandlerFinished() ) );

In the implementation of queryHandlerFinished() do whatever you want.

February 22, 2012

Andre Andre
Area 51 Engineer
6031 posts

Don’t forget to actually emit that signal too, for example at the end of your CQueryHandler::QueryFinished method:

  1. emit queryFinished();

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

February 22, 2012

p-himik p-himik
Lab Rat
255 posts

Oh, yes, forgot to mention. Thanks.

 
  ‹‹ [SOLVED] C:\QtSDK\QtCreator\bin\jom.exe" exited with code 2 - What does it mean?      Question regarding future of QWidget ››

You must log in to post a reply. Not a member yet? Register here!