Page 1 of 2 12 LastLast
Results 1 to 20 of 24

Thread: QNetworkRequest atribute

  1. #1
    Join Date
    Sep 2009
    Location
    Warsaw/Poland
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QNetworkRequest atribute

    How can I set atribute of network request ?
    I use this:

    Qt Code:
    1. ...
    2. QNetworkRequest request;
    3. request.setUrl(QUrl(strUrl));
    4. request.setAttribute(QNetworkRequest::User, strTest1);
    5. request.setAttribute(QNetworkRequest::UserMax, strCategory);
    6. accessManager->get(request);
    7. ...
    8.  
    9. ... net_finished(QNetworkReply *reply)
    10. {
    11. reply->deleteLater();
    12.  
    13. if (reply->error())
    14. return;
    15.  
    16. QString strTest1 = reply->attribute(QNetworkRequest::User).toString();
    17. QString strCategory = reply->attribute(QNetworkRequest::UserMax).toString();
    18. QByteArray bData = reply->readAll();
    19. ...
    To copy to clipboard, switch view to plain text mode 

    but in net_finished strTest1 and strCategory is empty .. why ?

  2. #2
    Join Date
    Oct 2010
    Location
    Belarus
    Posts
    71
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: QNetworkRequest atribute

    From QT Doc:


    QNetworkRequest::User. Special type. Additional information can be passed in QVariants with types ranging from User to UserMax. The default implementation of Network Access will ignore any request attributes in this range and it will not produce any attributes in this range in replies. The range is reserved for extensions of QNetworkAccessManager.
    Try read Qt documentation before ask stupid question.

  3. #3
    Join Date
    Sep 2009
    Location
    Warsaw/Poland
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkRequest atribute

    So how I can add my own attributes/data to request ?

  4. #4
    Join Date
    Oct 2010
    Location
    Belarus
    Posts
    71
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: QNetworkRequest atribute

    Create your own class from QNetworkAccessManager or extension. I don't known how hard it. But you can read Network Access src code
    Last edited by unit; 12th March 2011 at 14:15.
    Try read Qt documentation before ask stupid question.

  5. #5
    Join Date
    Sep 2009
    Location
    Warsaw/Poland
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkRequest atribute

    Quote Originally Posted by unit View Post
    Create your own class from QNetworkAccessManager. I don't known how hard it. But you can read QNetworkAccessManager src code
    It is too hard for too easy option. There should be another way to do this

  6. #6
    Join Date
    Oct 2010
    Location
    Belarus
    Posts
    71
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: QNetworkRequest atribute

    I don't kkown. QNetworkRequest::CustomVerbAttribute is only for request.

    Let's tell what is your goal?
    Try read Qt documentation before ask stupid question.

  7. #7
    Join Date
    Sep 2009
    Location
    Warsaw/Poland
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkRequest atribute

    Quote Originally Posted by unit View Post
    Let's tell what is your goal?
    I want to add 2 parameters to request, I will know then in finished() that kind of result reply contains, what kind of (my own) category

  8. #8
    Join Date
    Oct 2010
    Location
    Belarus
    Posts
    71
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: QNetworkRequest atribute

    Do you use many different request in one time in one class?
    Try read Qt documentation before ask stupid question.

  9. #9
    Join Date
    Sep 2009
    Location
    Warsaw/Poland
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkRequest atribute

    Quote Originally Posted by unit View Post
    Do you use many different request in one time in one class?
    yes, thats why i need to add 2 parameters, because I use many requests, and I need to know (my own) category of reply

  10. #10
    Join Date
    Oct 2010
    Location
    Belarus
    Posts
    71
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: QNetworkRequest atribute

    Ok. Sorry me, i'm not software developer, but i think problem in your code architecture.

    Try create class for each request. This class should do request and read reply. And have category or other attributes (and getters|setters ) I think it best way and solution.

    And also you can ask Mr. Wysota for best architecture solition. He is very good programmer.
    Try read Qt documentation before ask stupid question.

  11. #11
    Join Date
    Sep 2009
    Location
    Warsaw/Poland
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkRequest atribute

    Quote Originally Posted by unit View Post
    Ok. Sorry me, i'm not software developer, but i think problem in your code architecture.

    Try create class for each request. This class should do request and read reply. And have category or other attributes (and getters|setters ) I think it best way and solution.

    And also you can ask Mr. Wysota for best architecture solition. He is very good programmer.
    It's not architecture problem. Each request has my own named category, and I just want to add my variable to each request and read it in finished().

  12. #12
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QNetworkRequest atribute

    Quote Originally Posted by mero View Post
    It is too hard for too easy option. There should be another way to do this
    This is basic C++, it should not be too hard to implement. It is simple subclassing of existing object.

  13. #13
    Join Date
    Oct 2010
    Location
    Belarus
    Posts
    71
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: QNetworkRequest atribute

    What problem create class that do it? It will do request and have variable. It's more good and easy way as i think. Nut i'm not developer so i can mistake.
    Try read Qt documentation before ask stupid question.

  14. #14
    Join Date
    Sep 2009
    Location
    Warsaw/Poland
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkRequest atribute

    Quote Originally Posted by unit View Post
    What problem create class that do it? It will do request and have variable. It's more good and easy way as i think. Nut i'm not developer so i can mistake.
    Categories are dynamic so creating class for earch category is impossible and idiotic.
    There should be way to add my own string parameters to each request.

    Wysota help ! :-)

  15. #15
    Join Date
    Oct 2010
    Location
    Belarus
    Posts
    71
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: QNetworkRequest atribute

    Quote Originally Posted by mero View Post
    Categories are dynamic so creating class for earch category is impossible and idiotic.
    There should be way to add my own string parameters to each request.

    Wysota help ! :-)
    No class for every category. Create class that do request and read reaply and have variable category.

    And also try subclass QNetworkReply. I'm trying simple code for you, wait plz.
    Try read Qt documentation before ask stupid question.

  16. #16
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QNetworkRequest atribute

    Quote Originally Posted by mero View Post
    Categories are dynamic so creating class for earch category is impossible and idiotic.
    There should be way to add my own string parameters to each request.
    You can simply subclass the object and provide get and set for a string parameter (or enum or integer, whichever). This would take less than 5 minutes to code and provide the functionality you request.

  17. #17
    Join Date
    Oct 2010
    Location
    Belarus
    Posts
    71
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: QNetworkRequest atribute

    Oh. You are right. Subclass any access network class is hard job and take long time.

    And i known only two way:

    1. you add variable to URL if server can ignore it and than parse it from reply->url();

    2. or if you don't real need one connection access manager for all App, just create class that will be do request and read answer and have needed variables

    I can suggest something like attached code

    Plz, tell if attached code usefull for you, or should i find another solution
    Attached Files Attached Files
    Last edited by unit; 12th March 2011 at 18:45.
    Try read Qt documentation before ask stupid question.

  18. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkRequest atribute

    What do you need those extra values for? If they are to be parts of the request sent to the server then you should be using QNetworkRequest::setRawHeader(). If this is some kind of information you need for handling the reply then use QObject::setProperty() on the reply object you get when issuing a request to QNetworkAccessManager.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  19. The following 2 users say thank you to wysota for this useful post:

    mero (12th March 2011), unit (12th March 2011)

  20. #19
    Join Date
    Oct 2010
    Location
    Belarus
    Posts
    71
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: QNetworkRequest atribute

    QObject::setProperty() is good solution.
    Try read Qt documentation before ask stupid question.

  21. #20
    Join Date
    Feb 2011
    Posts
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QNetworkRequest atribute

    Quote Originally Posted by unit View Post
    From QT Doc:


    QNetworkRequest::User. Special type. Additional information can be passed in QVariants with types ranging from User to UserMax. The default implementation of Network Access will ignore any request attributes in this range and it will not produce any attributes in this range in replies. The range is reserved for extensions of QNetworkAccessManager.
    I am pretty certain that is simply saying that the library will not touch anyhthing you put in that range, not that it won't allow you to pass information through them. Hence their choice of name "User" for the variable. Attributes put there stay there, they just don't automatically get forwarded to the resultant QNetworkReply* object because QNetworkAccessManager doesn't know if it is supposed to or not.

    So you can still access it via `reply->request()->attribute(QNetworkRequest::User)`. Or you can insert it into the reply yourself:
    Qt Code:
    1. ...
    2. QNetworkRequest request;
    3. request.setUrl(QUrl(strUrl));
    4. request.setAttribute(QNetworkRequest::User, strTest1);
    5. request.setAttribute(QNetworkRequest::UserMax, strCategory);
    6. //accessManager->get(request);
    7. QNetworkReply* reply = accessManager->get(request);
    8. reply->setAttribute(QNetworkRequest::User, strTest1);
    9. reply->setAttribute(QNetworkRequest::UserMax, strCategory);
    10. ...
    11.  
    12. ... net_finished(QNetworkReply *reply)
    13. {
    14. reply->deleteLater();
    15.  
    16. if (reply->error())
    17. return;
    18.  
    19. QString strTest1 = reply->attribute(QNetworkRequest::User).toString();
    20. QString strCategory = reply->attribute(QNetworkRequest::UserMax).toString();
    21. //Or:
    22. //QString strTest1 = reply->request()->attribute(QNetworkRequest::User).toString();
    23. //QString strCategory = reply->request()->attribute(QNetworkRequest::UserMax).toString();
    24.  
    25. QByteArray bData = reply->readAll();
    26. ...
    To copy to clipboard, switch view to plain text mode 
    Both methods work for me at least. But I was just throwing code at a similar problem a few weeks ago until it started to function, so YMMV.

Similar Threads

  1. Replies: 1
    Last Post: 12th December 2010, 16:45
  2. Replies: 0
    Last Post: 3rd August 2010, 09:07
  3. QNetworkRequest file upload -- please help
    By Runtime Technologies in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2009, 15:55
  4. How to see the final request from QNetworkRequest
    By krippy2k in forum Qt Programming
    Replies: 13
    Last Post: 7th June 2009, 21:37
  5. Replies: 5
    Last Post: 20th January 2009, 14:11

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.