c++ - 从 QSettings 恢复 QList<bool>

标签 c++ qt

保存:

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/

相关文章:

python - QStyledItemDelegate (PySide/PyQt/Qt) 中 createEditor 的父级是什么?

qt - 如何防止用户调整 QTableWidget 列的大小?

c++ - Qt5.3(mingw32)中删除QQuickView的内存管理问题

c++ - 有没有办法在 QMap 值中设置一个类?

c++ - 地址分配给整型变量

c++ - 如何使 boost::lockfree::queue 动态调整大小

c++在调用函数时无法使用自定义类读取内存

没有默认构造函数的 C++ 私有(private)变量 - 无法编译?

c++ - 模板匹配两个(看似)不相关的类型

c++ - 命令在终端中工作,但不是通过 QProcess