Managing sessions and cookies

By default, WebEngine doesn't save any cookie and treats all user information as temporary sessions, which means when you close the program, your login session on the web page will automatically become invalid.

To enable cookies on Qt's WebEngine module, first add the following header to mainwindow.h:

#include <QWebEngineProfile> 

Then, simply call the following function to force persistent cookies:

QWebEngineProfile::defaultProfile()->setPersistentCookiesPolicy(QWebEngineProfile::ForcePersistentCookies);

After calling the preceding function, your login session will continue to exist after closing the program. To revert it to non-persistent cookies, we simply call:

QWebEngineProfile::defaultProfile()->setPersistentCookiesPolicy(QWebEngineProfile::NoPersistentCookies); 

Other than that, you can also change the directory in which your Qt program stores the cookies. To do that, add the following code to your source file:

QWebEngineProfile::defaultProfile()->setPersistentStoragePath("your folder");  

If, for some reason, you want to manually delete all the cookies, use the following code:

QWebEngineProfile::defaultProfile()->cookieStore()->deleteAllCookies();