如何在Qt 5.3中启用本地存储(LocalStorage)

4

我尝试了这种方法:

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

window.localStorage为true(不为null或undefined),但当我向localStorage中插入一个项目时:

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

发生了错误,Webkit Inspector控制台中的错误消息如下:

QuotaExceededError:DOM异常22:尝试添加超出配额的内容到存储中。

2个回答

6

今天整天我都在愤怒,因为重启应用后它不起作用了。所以我想这对某些人会有帮助:

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

注意 enablePersistentStorage。

3
我忘记了我已经启用了一个非常重要的开关:
settings->setAttribute(QWebSettings::PrivateBrowsingEnabled,true);

这将使浏览器进入私密模式,防止您向localStorage中插入值。但官方API文档没有提到这一点。
你只需要禁用开关即可解决问题:
settings->setAttribute(QWebSettings::PrivateBrowsingEnabled,false);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接