简体   繁体   中英

How to enable LocalStorage in Qt 5.3

I tried the method:

QWebSettings* settings = QWebSettings::globalSettings();
settings->setAttribute(QWebSettings::LocalStorageEnabled, true);
auto path = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
settings->setOfflineStoragePath(path);

window.localStorage is true(not null or undefined), but when I insert a item into the localStorage:

localStorage.setItem("b","isaac");
alert(localStorage["b"]);

The error is happened, and the error messages in the webkit inspector console are:

QuotaExceededError: DOM Exception 22: An attempt was made to add something to storage that exceeded the quota.

I was raging all day, because it was not working after restart of the app. So I think this will be helpfull for someone:

QWebSettings* settings = QWebSettings::globalSettings();
settings->setAttribute(QWebSettings::LocalStorageEnabled, true);
auto path = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
settings->setOfflineStoragePath(path);
settings->enablePersistentStorage(path);

Notice the enablePersistentStorage

I forgot I've enable a very important swithcer:

settings->setAttribute(QWebSettings::PrivateBrowsingEnabled,true);

This will set the browser to private mode and prevent you to insert value to localStorage. But the official api doc doesn't mention it.

You just set disable the switcher can solve the problem:

settings->setAttribute(QWebSettings::PrivateBrowsingEnabled,false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM