October 18, 2010

haithun haithun
Lab Rat
12 posts

Why is there no copying at QNetworCookieJar?

Page  
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.

25 replies

October 18, 2010

ZapB ZapB
Hobby Entomologist
1423 posts

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

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

October 18, 2010

haithun haithun
Lab Rat
12 posts

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

October 18, 2010

ZapB ZapB
Hobby Entomologist
1423 posts

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.

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

October 18, 2010

haithun haithun
Lab Rat
12 posts

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

October 19, 2010

Andre Andre
Robot Herder
6295 posts

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

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

October 19, 2010

haithun haithun
Lab Rat
12 posts

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).

October 19, 2010

Volker Volker
Ant Farmer
5428 posts

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

  1. MyNetworkCookieJar* MyNetworkCookieJar::getClone() const {
  2.     MyNetworkCookieJar *newJar = new MyNetworkCookieJar;
  3.     newJar->setAllCookies( allCookies() );
  4.     return newJar
  5. }

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

October 19, 2010

haithun haithun
Lab Rat
12 posts

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.

October 19, 2010

Volker Volker
Ant Farmer
5428 posts

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 :-)

October 19, 2010

haithun haithun
Lab Rat
12 posts

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

October 20, 2010

haithun haithun
Lab Rat
12 posts

Hi,
I realized that my problem description was insufficient and the problem has not been solved yet.
I want to do something like this:

  1. void Class::SetCookieList(const QList<QNetworkCookie> &Cookies)
  2. {
  3.     _CookieJar->setAllCookies(Cookies);
  4. }
  5.  
  6. QList<QNetworkCookie> Class::CookieList() const
  7. {
  8.     return _CookieJar->allCookies();
  9. }

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?

October 20, 2010

Deleted Member # 4a2 Deleted Member # 4a2
Ant Farmer
1481 posts

the docs say:

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

So should you extend QNetworkCookieJar class and override these methods?

October 20, 2010

Andre Andre
Robot Herder
6295 posts

haithun wrote:
Hi,
I realized that my problem description was insufficient and the problem has not been solved yet.
I want to do something like this:
  1. void Class::SetCookieList(const QList<QNetworkCookie> &Cookies)
  2. {
  3.     _CookieJar->setAllCookies(Cookies);
  4. }
  5.  
  6. QList<QNetworkCookie> Class::CookieList() const
  7. {
  8.     return _CookieJar->allCookies();
  9. }

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?


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.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

October 20, 2010

haithun haithun
Lab Rat
12 posts

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?

October 20, 2010

Andre Andre
Robot Herder
6295 posts

Why do you need more than one?

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

Page  
1

  ‹‹ Seg Fault when trying to display a QMessageBox      need help in QTableView ››

You must log in to post a reply. Not a member yet? Register here!