2014-02-21 24 views
2

恢復的QList 保存: 從QSettings

settings.setValue("profilesEnabled", QVariant::fromValue< QList<bool> >(profilesEnabled)); 

還原:

profilesEnabled = settings.value("profilesEnabled").toList()); //error 

但toList()還給的QVariant的QList,使用和profilesEnabled是布爾的的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 
+0

什麼是'settings'的格式?如果您使用'QSettings :: IniFormat',則無法將其保存到文件中。 –

回答

2

你的方法需要您實現流運營商使您的自定義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(); 
}