Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt WebKit
  4. [Solved] QNetworkAccessManager

[Solved] QNetworkAccessManager

Scheduled Pinned Locked Moved Qt WebKit
9 Posts 4 Posters 3.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    siguy
    wrote on last edited by
    #1

    Hi, I want to over ride the Get and Post methods in the QNetworkAccessManager class so i derived it with my own class but the Get and Post methods never get called is there something else i have to do ?? Thanks in advance.

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vsorokin
      wrote on last edited by
      #2

      Can you give more details, what do you want doing?

      --
      Vasiliy

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        The get and post methods of QNAM are not virtual. Perhaps that is what you are experiencing? Instead of reimplementing these operations, you should reimplement the protected virtual createRequest method. This method is called by the get, post and put methods.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          siguy
          wrote on last edited by
          #4

          Hi,
          Thanks for the replies. Yes the problem is get and post are not virtual but i don't think createRequest will work for me because i want to load some resources from a web page directly myself and let others go thru the default path. Like stylesheets etc.
          Im assuming it can't be done without having virtual get and put functions.

          Cheers Simon

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lgeyer
            wrote on last edited by
            #5

            It can be done as long as you assure that the non-virtual replaced methods are not called through a base class pointer.

            But why shoudn't QNetworkAccessManager::createRequest() be sufficient in your case? Just manipulate the QNetworkRequest and pass it up the the base class.

            @
            class NetworkAccessManager : public QNetworkAccessManager
            {
            protected:
            QNetworkReply* createRequest(Operation op, const QNetworkRequest& req, QIODevice* outgoingData = 0)
            {
            QNetworkRequest request(req);

                if(request.url().path() == "styles/style.css")
                {
                    QUrl url(request.url());
                    url.setPath("anotherStyle/style.css");
                    request.setUrl(url);
                }
            
                return QNetworkAccessManager::createRequest(op, request, outgoingData);
            }
            

            };
            @
            Brain to terminal. Not tested.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              siguy
              wrote on last edited by
              #6

              Hi,
              Yea the problem is i don't want to fetch the resource via a url hence passing the QNetworkRequest up to the base class will probably break the base class also i might have to do some extra processing on the resource before returning the http response to the browser.
              As far as i can tell the only way i can do this is to override the get/post functions etc.
              Im would not have to call the non-virtual methods through a bas class pointer, but i dont really understand how this could be done ??

              Thanks for your response.

              1 Reply Last reply
              0
              • L Offline
                L Offline
                lgeyer
                wrote on last edited by
                #7

                Well, get() and alike just call createRequest(), so by replacing get() instead of createRequest() you haven't won anything. There is no need to call the base class createRequest() implementation. Just wrap your resource in a QNetworkReply and make sure it emits the finished signal.
                @
                class NetworkAccessManager : public QNetworkAccessManager
                {
                protected:
                QNetworkReply* createRequest(Operation op, const QNetworkRequest& req, QIODevice* outgoingData = 0)
                {
                if(request.url().path() == "styles/style.css")
                {
                QNetworkReply* reply = createReplyForResource("anotherStyle/style.css");
                reply->emitFinishedSignal();

                        return reply;
                    }
                    else
                    { 
                        return QNetworkAccessManager::createRequest(op, request, outgoingData);  
                    }
                }
                

                };
                @
                As to your second question: You can replace a non-virtual method, but you cannot have polymorphism doing so. A small example:
                @
                class BaseClass
                {
                public:
                void printType() { qDebug() << "BaseClass"; }
                virtual void printTypeVirtual() { qDebug() << "BaseClass"; }
                };

                class DerivedClass : public BaseClass
                {
                public:
                void printType() { qDebug() << "DerivedClass"; }
                virtual void printTypeVirtual() { qDebug() << "DerivedClass"; }
                };

                BaseClass* baseClass = new BaseClass;
                baseClass->printType(); // "BaseClass"
                baseClass->printTypeVirtual(); // "BaseClass"

                DerivedClass* derivedClass = new DerivedClass;
                derivedClass->printType(); // "DerivedClass"
                derivedClass->printTypeVirtual(); // "DerivedClass"

                baseClass = derivedClass;
                baseClass->printType(); // "BaseClass", not "DerivedClass" as printType()
                // is not virtual and thus does not support polymorphism
                baseClass->printTypeVirtual(); // "DerivedClass", as expected
                @

                This means, if someone calls your replaced get() method through a NetworkAccessManager pointer the replaced implementation will be used, but if someone calls your replaced get() method through a QNetworkAccessManager pointer the original implementation will be used. See "here":http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.3.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  siguy
                  wrote on last edited by
                  #8

                  Hi,
                  Thanks for your reply, I can see how i can do it with a custom QNetworkReply object now. And thanks to everyone else who responded also, much appreciated.

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    lgeyer
                    wrote on last edited by
                    #9

                    You're welcome. Feel free to edit the thread title to "[Solved] ..." thus indicating that this topic has been solved and that there is a solution inside.

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved