[Qt-interest] QNetworkAccessManager

Girish Ramakrishnan girish at forwardbias.in
Sun Sep 20 08:13:07 CEST 2009


Lane Christiansen wrote:
> So I've set up a class that connects to and retrieves information from a 
> remote service. I'm using QNetworkAccessManager, and the get request is 
> performed inside my class' constructor. My class waits for the finished signal 
> from QNetworkReply, and then emits its own "finished" signal. If I declare an 
> instance of my class, and _then_ connect my class' finished signal, could 
> there be a race condition? It's seems like QNetworkAccessManager isn't 
> blocking my program, so I'm worried that if the remote service responds 
> quickly enough, the signals/slots won't be connected yet, and I won't know 
> that the request has finished.
> 

No, there is no race condition. Just make sure that you do not enter
Qt's event loop between the instantiation of your class and making the
connection (because the network operations are just queued in the event
loop). So, the below is fine.

> 13 MyClass *foo = new MyClass(params)
> 14 connect(foo, SIGNAL(done), this, SLOT(done))

Not fine:
> 13 MyClass *foo = new MyClass(params)
> 13.5 QMessageBox::aboutQt(0); // makes qt enter event loop
> 14 connect(foo, SIGNAL(done), this, SLOT(done))

Girish

> That probably made no sense at all, so here's some pseudocode:
> 
> 1  MY CLASS:
> 2  constructor(params)
> 3  {
> 4  	perform get request
> 5  	connect(reply, SIGNAL(finished()), this, SLOT(finished()))
> 6  }
> 7  finished()
> 8  {
> 9  	emit done()
> 10 }
> 11 
> 12 IMPLEMENTATION:
> 13 MyClass *foo = new MyClass(params)
> 14 connect(foo, SIGNAL(done), this, SLOT(done))



More information about the Qt-interest-old mailing list