MistyD
MistyD

Reputation: 17223

Qt : Best approach to wait for a signal from a function

I currently have something like this

void foo::replyFinished(QNetworkReply* rply)
{
     .....
     .....
}

void foo::ReceiveReport()
{
  //Communicate with http
   QUrl url(url_complete.c_str());
   QNetworkRequest request;
   request.setUrl(url);
   currentReply = networkManager->get(request);
}


void foo::mymethodA()  //Start from here
{
   ReceiveReport();
   -----------------> Wait();
   MoreStuff();
}

Now what happens is that I call mymethodA. This method called ReceiveReport. The receive report has a signal that connects to replyFinished(). Now what I want is to wait until I have received a reply from network and this happens when replyFinished is called.Currently the problem is that the method MoreStuff() is called before the method replyFinished() is triggered. My question is how can I make it wait till i received a reply ? All of these are running in the same thread

Upvotes: 2

Views: 3331

Answers (1)

Marcel Blanck
Marcel Blanck

Reputation: 866

This can not work without an ugly processEvent () hack. What you need is a slot that does the processing. Signals and slots in Qt only work if you leave a method and give qt the time to run it's event loop.

Another way would be the use of different threads.

You have NO C# async and await in qt, let that paradigma go.

Please read the docs about the Qt event loop again to get a better understanding and then force yourself to event based programming, because that is what qt has to offer and how it works as soon as you run in QApplication exec ()

Upvotes: 1

Related Questions