Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why is there no copying at QNetworCookieJar?
QtWS25 Last Chance

Why is there no copying at QNetworCookieJar?

Scheduled Pinned Locked Moved General and Desktop
26 Posts 4 Posters 12.0k 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.
  • H Offline
    H Offline
    haithun
    wrote on last edited by
    #1

    Hi,

    I think the title says everything
    there is the "Q_DISABLE_COPY(QNetworkCookieJar)" thing in the sourcecode and I and a friend which is a much better programmer than me were not able to find a reason for that.
    My program would be much better if I could use the copy constructor.
    Hope somebody can tell me.

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      ZapB
      wrote on last edited by
      #2

      Because QNetworkCookieJar inherits from QObject and QObject cannot be copied because of parenting issues (amongst others).

      Nokia Certified Qt Specialist
      Interested in hearing about Qt related work

      1 Reply Last reply
      0
      • H Offline
        H Offline
        haithun
        wrote on last edited by
        #3

        That could not be the reason cause QNetworkAccessManager also inherits QObject and can be copied.

        1 Reply Last reply
        0
        • Z Offline
          Z Offline
          ZapB
          wrote on last edited by
          #4

          No it cannot. There is no explicitly defined copy constructor or assignment operator so it will try to use a default member-wise one created by the compiler which will fail when it tries to recurse to the base class' (QObject's) copy constructor or assignment operator.

          Nothing derived from QObject can be copied because QObject marks its copy constructor and assignment operator as private.

          Nokia Certified Qt Specialist
          Interested in hearing about Qt related work

          1 Reply Last reply
          0
          • H Offline
            H Offline
            haithun
            wrote on last edited by
            #5

            But do it work for QNetworkAccessManager wich also inherits QObject and has no copy constructor?

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

              If you can copy a QNetworkAccessManager, then I would considder that a Qt bug. Don't (try to) copy QObject or QObject derived objects.

              1 Reply Last reply
              0
              • H Offline
                H Offline
                haithun
                wrote on last edited by
                #7

                Does anybody then know how to get a object inheriting QNetworkCookieJar into a QNetworkObjectJar variable? Currently I am using a <reinterpretate_cast> to do it (this works cause my class has no member varibales just two functions).

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #8

                  You could subclass QNetworkObjectJar, and implement a clone function like this:

                  @
                  MyNetworkCookieJar* MyNetworkCookieJar::getClone() const {
                  MyNetworkCookieJar *newJar = new MyNetworkCookieJar;
                  newJar->setAllCookies( allCookies() );
                  return newJar
                  }
                  @

                  But watch out: you must delete the returned pointer yourself. There is NO way of treating QObject derived classes as value types!

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • H Offline
                    H Offline
                    haithun
                    wrote on last edited by
                    #9

                    That was the idea I also had first but keep in mind that that the CooieJar also can connect some cookies with an URI. This connections will be lost if do like you discribed.

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      goetz
                      wrote on last edited by
                      #10

                      Class QNetworkCookie contains the URI the cookie is bound to. According to the API docs the methods allCookies() and setAllCookies() can be used to implement a permanent cookie storage. So I think the returned list of cookies contains all the necessary information. If not, I'd definitely call this a bug :-)

                      http://www.catb.org/~esr/faqs/smart-questions.html

                      1 Reply Last reply
                      0
                      • H Offline
                        H Offline
                        haithun
                        wrote on last edited by
                        #11

                        Your right the data is being set up in QNetworkCookie. So your funtion will work and I am happy now that I do not have to use this cast. :) Thanks

                        1 Reply Last reply
                        0
                        • H Offline
                          H Offline
                          haithun
                          wrote on last edited by
                          #12

                          Hi,
                          I realized that my problem description was insufficient and the problem has not been solved yet.
                          I want to do something like this:
                          @void Class::SetCookieList(const QList<QNetworkCookie> &Cookies)
                          {
                          _CookieJar->setAllCookies(Cookies);
                          }

                          QList<QNetworkCookie> Class::CookieList() const
                          {
                          return _CookieJar->allCookies();
                          }@

                          But since the functions "setAllCookies" and "allCookies" are protected I am unable to do so. What is the proper way to do what I want to do?

                          1 Reply Last reply
                          0
                          • ? This user is from outside of this forum
                            ? This user is from outside of this forum
                            Guest
                            wrote on last edited by
                            #13

                            the docs say:

                            bq.
                            This function is suitable for derived classes to save cookies to disk, ....

                            So should you extend QNetworkCookieJar class and override these methods?

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

                              [quote author="haithun" date="1287528028"]Hi,
                              I realized that my problem description was insufficient and the problem has not been solved yet.
                              I want to do something like this:
                              @void Class::SetCookieList(const QList<QNetworkCookie> &Cookies)
                              {
                              _CookieJar->setAllCookies(Cookies);
                              }

                              QList<QNetworkCookie> Class::CookieList() const
                              {
                              return _CookieJar->allCookies();
                              }@

                              But since the functions "setAllCookies" and "allCookies" are protected I am unable to do so. What is the proper way to do what I want to do?[/quote]
                              If all you want to do is access those methods from an outside class, then just make a trivial subclass of QNetworkCookieJar and make those two method public. You can then access them from your external class. If what you want to do really belongs to the cookie jar itself, perhaps implementing your functionality in that subclass itself would be a better choice. It may help to keep encapsulation.

                              1 Reply Last reply
                              0
                              • H Offline
                                H Offline
                                haithun
                                wrote on last edited by
                                #15

                                That was also my idea but I have the problem that I want to write the data of a normal QNetworkCookieJar to my class which is quite difficult without a copyconstructor.
                                Anybody knowing a good solution?

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

                                  Why do you need more than one?

                                  1 Reply Last reply
                                  0
                                  • H Offline
                                    H Offline
                                    haithun
                                    wrote on last edited by
                                    #17

                                    I dont need more than one but I want to give my function "SetCookieJar" a QNetworkCookie. Thats data should be saved in my extended class. I am doing this because I want to avoid to have two member variables and the different treatment in my functions.

                                    1 Reply Last reply
                                    0
                                    • G Offline
                                      G Offline
                                      goetz
                                      wrote on last edited by
                                      #18

                                      So as Andre wrote - subclass QNetworkCookieJar, make the two protected functions public and use them to transfer the cookie data. No need for a copy constructor as far as I see. Left alone that a copy constructor would do quite the same.

                                      @
                                      this->setAllCookies(other->allCookies());
                                      @

                                      Or implement a cloneFromOther Method to simulate an assignment operator/copy constructor:

                                      @
                                      void MyCookieJar::cloneFromOther(const QNetworkCookieJar *other)
                                      {
                                      if(other)
                                      setAllCookies(other->allCookies())
                                      }
                                      @

                                      As mentioned before, QNetworkCookieJar is a QObject and there simply IS NO copy constructor. It does not help to ask again and again - the trolls will not add it :-)

                                      http://www.catb.org/~esr/faqs/smart-questions.html

                                      1 Reply Last reply
                                      0
                                      • H Offline
                                        H Offline
                                        haithun
                                        wrote on last edited by
                                        #19

                                        I understood that there is no copy cunstructor and also the reason.
                                        Well your code is very similar to the one that I wrote cause subclassing QNetworkCookieJar was also my idea. But your version wont work, because you try to call the protected "allCookies()" function of the "other" object as if it's public. Would it be public my problem would not exist.

                                        1 Reply Last reply
                                        0
                                        • G Offline
                                          G Offline
                                          goetz
                                          wrote on last edited by
                                          #20

                                          If you derive class MyCookieJar from QNetworkCookieJar you have access to the protected methods of the base class QNetworkCookieJar. And of course you have access to the protected methods of the other object pointer in that method.

                                          http://www.catb.org/~esr/faqs/smart-questions.html

                                          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