Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get function which initiated QNetworkRequest in replyFinished()

Tags:

c++

qt

i have following problem:

I have a class Foo which encapsulates a web-api. the interface has following functions:

Foo::addItem( QString id )
Foo::updateItem( QString id )

both function initiate a QNetworkRequest with the same URL but the usage of the data is different. Therefore i need to know in slot function Foo::replyFinished( QNetworkReply *wf_reply ) from where the QNetworkRequest originated.

How would you solve this?

I could use variable to store the adress of the QNetworkRequest to compare it later to wf_reply->request() but this seems like a hack to me. Considering you can call addItem() or updateItem() hundred times before replyFinished() is executed for the first time. The best way would be to add a sting or integer to QNetworkRequest which contains the function name or id.

like image 455
Julian Avatar asked Mar 19 '11 20:03

Julian


1 Answers

In your original QNetworkRequest you can set an attribute with

setAttribute(Attribute code, const QVariant & value)

Attribute is an enum and there is a reserved code for just this situation, QNetworkRequest::User. (See: Attribute)

In your QNetworkReply, you can pull the QNetworkRequest with request() then get the Attribute from there with attribute()

Bit of a hack, but I think it should work.

like image 151
Brian Roach Avatar answered Oct 24 '22 03:10

Brian Roach