c++qt6qwebenginepageqwebengine

Why aren’t cookies and cache persisting in QWebEngineProfile (Qt 6.8.1)?


I’m currently developing a browser-like application using Qt 6.8.1 and C++. My goal is to enable persistent cookies and cache so that website logins (e.g., Gmail or Microsoft accounts) remain active after restarting the application. However, despite my setup, cookies are not preserved, and all logins are forgotten.

I’ve implemented a QWebEngineProfile with a persistent storage path and cache path, using ForcePersistentCookies. I’ve also enabled local storage and other relevant settings. Below is my attempt:

QWebEngineProfile *PersistentProfile = new QWebEngineProfile(this);
QString storagePath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/PersistentProfile");

if (!QDir(storagePath).exists()) {
    QDir().mkpath(storagePath);
}

PersistentProfile->setPersistentStoragePath(storagePath);
PersistentProfile->setCachePath(storagePath);
PersistentProfile->setPersistentCookiesPolicy(QWebEngineProfile::ForcePersistentCookies);
PersistentProfile->settings()->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);
PersistentProfile->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, true);
PersistentProfile->settings()->setAttribute(QWebEngineSettings::XSSAuditingEnabled, false);

QWebEnginePage *profilepage = new QWebEnginePage(PersistentProfile, this);
QWebEngineView *webView = new QWebEngineView(this);
webView->setPage(profilepage);

I’ve reviewed GitHub bug reports but couldn’t determine if this behavior is a known limitation of QWebEngineProfile in Qt 6.8.1 or if there’s an issue with my code.

Questions:

I’m relatively new to C++ and Qt programming, so I’d appreciate any guidance or resources to help me understand this better.


Solution

  • Using the advice given by @IgorTandetnik along with a bit of tinkering, I was able to solve my own question.

    The main issue stems from defining the QWebEngineProfile constructor without the storageName parameter, which results in a off-the-record profile, rather than one that would hold persistent cache and cookies. So, rather than incorrectly using QWebEnginePage *profilepage = new QWebEnginePage(PersistentProfile, this);, as it does not contain the optional storageName parameter, it is correct to define the constructor as QWebEngineProfile *PersistentProfile = new QWebEngineProfile(QStringLiteral("PersistentProfile"), this);, which does.

    Further, the second issue I found in my code originates in how I defined the storage path. In my testing, QStandardPaths::AppDataLocation was not accessible to the application while in a build environment under my IDE. Rather, I had to specify a direct, full path to a folder in the build directory for the application to find the correct storage location. From my research, when publishing and bundling the application, however, using QStandardPaths::AppDataLocation is correct, but does not seem to work in a build environment.