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

Get Hands-On GUI Programming with C++ and Qt5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.