Search code examples
qtqt5qnetworkaccessmanager

Difference between QNetworkRequest::User and QNetworkRequest::UserMax


I found QNetworkRequest::User and QNetworkRequest::UserMax in the documentation of QNetworkRequest, but I cannot understand the meaning of each one according to the explanation found.

What is the meaning of each one and what is the difference between them?


Solution

  • First, both are used to set attributes to a QNetWorkRequest, via the method setAttribute(Attribute code, const QVariant &value). For example, if you have two functions which initiate a QNetworkRequest with the same URL but they use the data differently, and that you need to know from where the QNetworkRequest originated, then you will need to use an attribute.

    First, you set the attribute to your request :

    request->setAttribute(QNetworkRequest::User, QVariant("myRequest")); 
    

    And then you get your request back thanks to the attribute you put in the first place :

    if(reply->request().attribute(QNetworkRequest::User).toString().contains("myRequest"))
    // ...
    

    BUT : you will need your own implementation to use special type attributes. As you might have read in the documentation :

    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.

    In short, you will need to subclass QNetworkAccessManager to use QNetworkRequest::User and QNetworkRequest::UserMax attributes. Here's a very simple example :

    class MyNetworkAccessManager : public QNetworkAccessManager 
    {
        public:
        enum myAttribute { Attribute1 = QNetworkRequest::User, Attribute2 };
    
        protected:
    
        QNetworkReply * createRequest ( Operation op, const QNetworkRequest & request, QIODevice * data = 0 ) 
        {
            QNetworkReply *reply = QNetworkAccessManager::createRequest(op, request, data);
            if(request.attribute(Attribute1).isValid()) reply->setAttribute(Attribute1, request.attribute(Attribute1));
            if(request.attribute(Attribute2).isValid()) reply->setAttribute(Attribute2, request.attribute(Attribute2));
            return reply;
         }
     };
    

    Now, about the difference between the two of them : there is none. They are the same thing. QNetworkRequest::UserMax is just an upper bound.

    Indeed, QNetworkRequest::Attribute is an enum, and you are provided a full range of custom values, going from QNetworkRequest::User = 1000 to QNetworkRequest::UserMax = 32767. So you can do this when you set attributes to your requests :

    request->setAttribute(QNetworkRequest::User, QVariant("myRequest")); 
    request->setAttribute(QNetworkRequest::User + 1, QVariant("myRequest1"));
    request->setAttribute(QNetworkRequest::User + 100, QVariant("myRequest100"));
    request->setAttribute(QNetworkRequest::UserMax, QVariant("lastRequest"));
    

    NB : Although attributes seem useful, you might want to use setProperty(const char *name, const QVariant &value) for most cases. Indeed, attributes are not there to provide storage for custom data, unlike dynamic properties.