如何在Qt5.3中启用LocalStorage
我试过这个方法:如何在Qt5.3中启用LocalStorage,qt,webkit,local-storage,Qt,Webkit,Local Storage,我试过这个方法: QWebSettings* settings = QWebSettings::globalSettings(); settings->setAttribute(QWebSettings::LocalStorageEnabled, true); auto path = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); settings->setOfflineStoragePat
QWebSettings* settings = QWebSettings::globalSettings();
settings->setAttribute(QWebSettings::LocalStorageEnabled, true);
auto path = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
settings->setOfflineStoragePath(path);
window.localStorage为true(不是null或未定义),但当我将项目插入localStorage时:
localStorage.setItem("b","isaac");
alert(localStorage["b"]);
发生错误,webkit inspector控制台中的错误消息如下:
QUOTAEEXCEEDERROR:DOM异常22:试图向存储中添加超出配额的内容
我忘了我启用了一个非常重要的开关:
settings->setAttribute(QWebSettings::PrivateBrowsingEnabled,true);
这会将浏览器设置为私有模式,并阻止您向localStorage插入值。但是官方的api文档没有提到这一点
您只需设置禁用开关即可解决问题:
settings->setAttribute(QWebSettings::PrivateBrowsingEnabled,false);
我愤怒了一整天,因为重启应用程序后它就不工作了。 因此,我认为这将对某些人有所帮助:
QWebSettings* settings = QWebSettings::globalSettings();
settings->setAttribute(QWebSettings::LocalStorageEnabled, true);
auto path = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
settings->setOfflineStoragePath(path);
settings->enablePersistentStorage(path);
请注意enablePersistentStorage