Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
SumiSadiq
Posts: 141
Registered: ‎04-08-2013
My Device: Blackberry 10
My Carrier: Blackberry
Accepted Solution

append a variable along withQNetworkRequest

Hi,

  Is there any way to append a variable along with the QNetworkRequest and receive that variable along with the QNetworkResponse??

 

 

Plz help me out

Please use plain text.
Developer
Zmey
Posts: 1,510
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: append a variable along withQNetworkRequest

[ Edited ]

Hi,

 

Yes, it's possible to pass any information wrapped in QVariant using setAttribute with attribute IDs starting from QNetworkRequest::User to QNetworkRequest::UserMax.

 

QNetworkRequest request(QUrl(apiURL));
...
int offset = 12345;
request.setAttribute(QNetworkRequest::User, offset); // primitive types are converted to QVariant automatically
request.setAttribute(QNetworkRequest::User+1, someOtherVar);

 To retrieve it later:

 

void ServerRequestManager::processResponse()
{
  QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());

  int offset = reply->request().attribute(QNetworkRequest::User).toInt();
  ...

  reply->deleteLater();
}

 

 I suggest declaring an enum for attribute ids in .h file:

 

class ServerRequestManager: public QObject
{
public:
enum { AttributeOffset = QNetworkRequest::User, AttributeVar2, AttributeVar3 };
... };

 

 


Andrey Fidrya, @zmeyc on twitter
Please use plain text.
Developer
SumiSadiq
Posts: 141
Registered: ‎04-08-2013
My Device: Blackberry 10
My Carrier: Blackberry

Re: append a variable along withQNetworkRequest

Thank u very much for your reply .

I tried the following

request.setUrl(QUrl(correctImagePath));
	request.setAttribute(AttributeOffset, tableName); // primitive types are converted to QVariant automatically
	request.setAttribute(AttributeVar2, tableId);


void Home::replyFinished1(QNetworkReply* reply) {

	// Get reply
	//QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());

	 QString tableName = reply->request().attribute(AttributeOffset).toString();
	 QString tableId = reply->request().attribute(AttributeVar2).toString();
	 qDebug()<<"tableNmae =" + tableName;
	 qDebug()<<"tableId =" + tableId;
}

 But Iam getting a compilation error

 

error: no matching function for call to 'QNetworkRequest::setAttribute(Home::<anonymous enum>, QString&)'.

 

But when I tried with 

QNetworkRequest::User

instead of enum, it worked. But even then ,when I used 

reply->request().attribute(QNetworkRequest::User+1).toString()

 it did not work for my second variable. 

 

Any tips?

Please use plain text.
Developer
SumiSadiq
Posts: 141
Registered: ‎04-08-2013
My Device: Blackberry 10
My Carrier: Blackberry

Re: append a variable along withQNetworkRequest

Never mind. solved it by using like this.

 

request.setAttribute(QNetworkRequest::Attribute(AttributeOffset), tableName);

 Thank you. :smileyhappy:

Please use plain text.