Search code examples
c++qtqtwebengine

QtWebEngine, How to Save Cookies in C++/Qt6.2.4?


Since I change Qt5 to Qt6 for my app,

What worked for Qt5 (to save the cookies, I followed this thread QT 5.6 QWebEngine doesn't save cookies),

Doesn't work now :

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

    defaultProfile->setHttpCacheType(QWebEngineProfile::DiskHttpCache);
    defaultProfile->setPersistentCookiesPolicy(QWebEngineProfile::ForcePersistentCookies);
    QHttpPart* header = new QHttpPart;
    header->setRawHeader("X-Frame-Options", "ALLOWALL");

    defaultProfile->setCachePath(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
    defaultProfile->setPersistentStoragePath(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation));

I need to save Cookies.


Solution

  • The Solution is :

        QWebEngineProfile *profile = new QWebEngineProfile(QString::fromLatin1("MyApplication.%1").arg(qWebEngineChromiumVersion()));  // unique profile store per qtwbengine version
        QWebEnginePage *page = new QWebEnginePage(profile); // page using profile
        QWebEngineView *view = new QWebEngineView();
    
        view->setPage(page);
        view->setUrl(AccueilUrl);
        view->setZoomFactor(1.2);
    
        setCentralWidget(view);