Qt WebKit and Permanent Cookies
Asked Answered
D

3

16

I have a mainwindow, in it, I have a single QWebView. I want to make a certain website (www.kdkdkkd.com for instance) cookies permanent, so each time a user closes and repopens the browser he doesnt need to log in again.

Thanks,

Derryberry answered 23/3, 2011 at 14:4 Comment(0)
O
26

You need to set your own "Cookie Jar".

  1. Use QNetworkAccessManager::setCookieJar .
  2. Extend QNetworkCookieJar - as it doesn't persist cookies by default when browser is closed:

Once the QNetworkCookieJar object is deleted, all cookies it held will be discarded as well. If you want to save the cookies, you should derive from this class and implement the saving to disk to your own storage format. (From QNetworkCookieJar documentation.)

You can do this either by pushing to permanent storage on each QNetworkCookieJar::setCookiesFromUrl (safest but slowest) or only when the browser window is closed (faster overall but less safe as if you exit abnormally cookie jar won't be saved).

Note that some cookies are not supposed to be persisted, you can use QNetworkCookie::isSessionCookie to determine this.

Oneirocritic answered 23/3, 2011 at 15:8 Comment(2)
I Believe you meant: QNetworkCookieJar::setCookiesFromUrl I will check this now.Derryberry
can you please explain with the code, I unable to store the cookiesBombay
D
4

There is an official Qt Demo, called "Browser". It implements a CookieJar class, which supports save and load of cookies to an .ini file, using the QSettings stack.

Discontinuation answered 13/3, 2013 at 10:44 Comment(0)
U
1

Once cookie(s) registered in the NAM (NetworkAccessManager), set the webview to use it :

myWebView->page()->setNetworkAccessManager(myNAM);

Utah answered 19/7, 2012 at 13:26 Comment(1)
Consider improving the formatting and being a bit descriptive.Security

© 2022 - 2024 — McMap. All rights reserved.