gmlt.A
gmlt.A

Reputation: 357

Get custom QNetworkRequest object from QNetworkReply

Here's the code I've got:

UserInfoRequest request(token);
QNetworkReply* reply = network->sendCustomRequest(request, request.getVerb());

Where UserInfoRequest derives from another class:

class APIRequest : public QNetworkRequest

So the question is: can I somehow cast QNetworkRequest object which was returned by QNetworkReply::request() to my APIRequest class? If it is impossible, then what is the best way to pass my custom request object from code listed before to reply handler slot.

Upvotes: 1

Views: 421

Answers (1)

Nejat
Nejat

Reputation: 32635

This is not a good idea to assign a base class to a derived one. That's because APIRequest has everything belonging to QNetworkReply but QNetworkReply does not contain some extra features of APIRequest. So when you assign QNetworkReply to APIRequest, the assignment operator of QNetworkReply is used and al of the extra data of APIRequest remains untouched.

In case you really want want to do this, you should have an assignment operator like:

class APIRequest : public QNetworkReply 
{
    APIRequest& operator= (const QNetworkReply& reply) 
    { 
       ... 
    }
}

In which you copy members of QNetworkReply and somehow deal with the extra information in the derived type APIRequest. Then you can directly assign QNetworkReply::request() to an object of APIRequest.

Upvotes: 1

Related Questions