保存:
settings.setValue("profilesEnabled", QVariant::fromValue< QList<bool> >(profilesEnabled));
恢复:
profilesEnabled = settings.value("profilesEnabled").toList()); //error
但是 toList() 返回一个 QVariant 的 QList,而profilesEnabled 是一个 bool 的 QList。
有什么优雅的方式来转换它吗? (我可以迭代 QVariant 的 QList 并进行一一转换)
更新:
QVariant var = QVariant::fromValue< QList< bool > >(profilesEnabled);
settings.setValue("profilesEnabled", var);
第二行运行时崩溃:
QVariant::save: unable to save type 'QList<bool>' (type id: 1031).
ASSERT failure in QVariant::save: "Invalid type to save", file kernel\qvariant.cpp, line 1966
最佳答案
您的方法要求您实现流运算符,以使自定义 QVariant
类型的序列化成为可能。我建议将您的数据转换为 QVariantList
。
保存:
QVariantList profilesEnabledVariant;
foreach(bool v, profilesEnabled) {
profilesEnabledVariant << v;
}
settings.setValue("profilesEnabled", profilesEnabledVariant);
加载中:
profilesEnabled.clear();
foreach(QVariant v, settings.value("profilesEnabled").toList()) {
profilesEnabled << v.toBool();
}
关于c++ - 从 QSettings 恢复 QList<bool>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21923016/